Search code examples
pythonregexstring-parsing

Substitute zero for missing values in python


I have a text file with the following entries:

 G1 X-387.7 A3=-1.0 B3=0.0 C3=0.0 F=R3 
 Y1.824 Z264.663 A3=-1.0 C3=0.0 

I extracted the numbers(numerical equivalents) of the variable alphabets using the code

 import re
 with open('input file.txt','r') as file:
 for line in file:
    print re.findall("(?<=[AZaz])?(?!\d*=)[0-9.+-]+",line)

I got the results as follows:

   ['-387.7', '-1.0', '0.0', '0.0', '3']
   ['1.824', '264.663', '-1.0', '0.0']

But I need only the values corresponding to x,y,z,a3,b3,c3. All other variables should be neglected. How to selectively remove other variables and substitute 0 in the plac of missing variable(x,y,z,a3,b3,c3)?


Solution

  • This code will match the numbers immediately after X, Y, Z, A3, B3 and C3. If any of the strings in the required list are missing from the input, it will set them to 0.0.

    required = ['X', 'Y', 'Z', 'A3=', 'B3=', 'C3=']    
    matches = re.findall(r"(X|Y|Z|A3=|B3=|C3=)(\-?\d+(?:\.\d+)?)", line)
    
    for var in required:
        if var not in [v for v, val in matches]:
            matches.append((var, '0.0'))
    

    Sample input: G1 X-387.7 A3=-1.0 B3=0.0 C3=0.0 F=R3 (Y and Z are missing)

    Result:

    [('X', '-387.7'), ('A3=', '-1.0'), ('B3=', '0.0'), ('C3=', '0.0'), ('Y', '0.0'), ('Z', '0.0')]
    

    You can test it here: https://regex101.com/r/CEUCpU/4