在python中用零替换缺失值

时间:2017-11-07 17:52:51

标签: python regex string-parsing

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

 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的值。所有其他变量都应该被忽略。如何有选择地删除其他变量并在缺失变量(x,y,z,a3,b3,c3)的plac中替换0?

3 个答案:

答案 0 :(得分:0)

此代码将匹配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

答案 1 :(得分:0)

以下是从每个文字行获取所需内容的另一种方法。我已经指定应该选择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']

答案 2 :(得分: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))