用零替换 python 中的缺失值

Substitute zero for missing values in python

我有一个包含以下条目的文本文件:

 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 

我使用代码

提取了变量字母表的数字(数字等价物)
 import re
 with open('input file.txt','r') as file:
 for line in file:
    print re.findall("(?<=[AZaz])?(?!\d*=)[0-9.+-]+",line)

我得到的结果如下:

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

但我只需要x,y,z,a3,b3,c3对应的值。应忽略所有其他变量。如何有选择地删除其他变量并用 0 代替缺少的变量(x,y,z,a3,b3,c3)?

此代码将匹配紧跟在 X、Y、Z、A3、B3 和 C3 之后的数字。如果 required 列表中的任何字符串从输入中丢失,它将把它们设置为 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'))

示例输入:G1 X-387.7 A3=-1.0 B3=0.0 C3=0.0 F=R3(缺少 Y 和 Z)

结果:

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

你可以在这里测试:https://regex101.com/r/CEUCpU/4

这是从每个文本行中获取所需内容的另一种方法。我已指定应选择 x、y、z 或 A3、B3、C3 之后的 (+/-) 位小数。

import re
file = "/path/to/text.txt"
with open(file,'r') as infile:
    for line in infile:
        print(line)
        print(re.findall("[XYZABC3=](-?\d+\.\d+)", line))

打印如下:

G1 X-387.7 A3=-1.0 B3=0.0 C3=0.0 F=R3 

['-387.7', '-1.0', '0.0', '0.0']

 Y1.824 Z264.663 A3=-1.0 C3=0.0

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

我会避免在这里使用正则表达式,而只是用字符串操作来解析它:

def read_commands(data):
    last_command = []

    for token in data.split():
        if token.startswith('G') and token[1:].isnumeric():
            if last_command:
                yield last_command

            last_command = [token]
        else:
            last_command.append(token)

    yield last_command


def parse_command(command):
    code = command[0]
    args = {}

    for arg in command[1:]:
        if '=' in arg:
            key, value = arg.split('=')
        else:
            key = arg[0]
            value = arg[1:]

        args[key] = value

    return code, args

if __name__ == '__main__':
    data = '''
        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


        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

        G1 X0 Y0 Z0
    '''

    for command in read_commands(data):
        code, args = parse_command(command)

        print(args.get('A3', 0))