在Python中更改文件

在Python中更改文件,python,file,g-code,Python,File,G Code,最近,Iv'e开始用Python编程。 我对翻译几行G代码感兴趣: G1 F1200 X253.644 Y174 Z0.2 E3.05044 G1 X252.388 Y174.497 Z0.2 E3.06822 G1 X251.084 Y174.685 Z0.2 E3.08557 到以下格式的txt文件: GoTo(X(253.644), Y(174), Z(0.2)) GoTo(X(252.388), Y(174.497), Z(0.2)) GoTo(X(251.084), Y(174.68

最近,Iv'e开始用Python编程。 我对翻译几行G代码感兴趣:

G1 F1200 X253.644 Y174 Z0.2 E3.05044
G1 X252.388 Y174.497 Z0.2 E3.06822
G1 X251.084 Y174.685 Z0.2 E3.08557
到以下格式的txt文件:

GoTo(X(253.644), Y(174), Z(0.2))
GoTo(X(252.388), Y(174.497), Z(0.2))
GoTo(X(251.084), Y(174.685), Z(0.2))
我不参与G代码的E和F参数。 我试过这个密码:

with open("C:/Users/ZoharMa/Documents/G code/draft_of_g_code.txt" , 'r') as f:
    for line in f:
        line = line.strip()
        if 'G1' in line:
                new_line =  f'GoTo(X({line.split()[1][1:]}), Y({line.split()[2][1:]}), Z({line.split()[3][1:]}))'
                print(new_line)
                with open("copy.txt", "w") as file:
                    file.write(new_line)
             
但是我创建的文件(“复制”)只包含最后一行!(我对所有线路都感兴趣)
我将感谢任何帮助!非常感谢

您正在覆盖每行的输出文件。(另外,
file.write()
不添加换行符,因此切换到
print(…,file=…)

您可以使用语句在一个
中同时打开输入和输出文件,如下所示:

with open("C:/Users/ZoharMa/Documents/G code/draft_of_g_code.txt" , 'r') as input_file, open("copy.txt", "w") as output_file:
    for line in input_file:
        line = line.strip()
        if 'G1' in line:
                new_line =  f'GoTo(X({line.split()[1][1:]}), Y({line.split()[2][1:]}), Z({line.split()[3][1:]}))'
                print(new_line)
                print(new_line, file=output_file)
但是,将新行存储在内存中并在末尾写入可能更安全:

new_lines = []
with open("C:/Users/ZoharMa/Documents/G code/draft_of_g_code.txt", "r") as input_file:
    for line in input_file:
        line = line.strip()
        if "G1" in line:
            new_line = f"GoTo(X({line.split()[1][1:]}), Y({line.split()[2][1:]}), Z({line.split()[3][1:]}))"
            print(new_line)
            new_lines.append(new_line)

with open("copy.txt", "w") as output_file:
    for line in new_lines:
        print(line, file=output_file)

这样,如果程序失败,您就不会得到一个写了一半的文件。

您正在覆盖每行的输出文件。(另外,
file.write()
不添加换行符,因此切换到
print(…,file=…)

您可以使用
语句在一个
中同时打开输入和输出文件,如下所示:

with open("C:/Users/ZoharMa/Documents/G code/draft_of_g_code.txt" , 'r') as input_file, open("copy.txt", "w") as output_file:
    for line in input_file:
        line = line.strip()
        if 'G1' in line:
                new_line =  f'GoTo(X({line.split()[1][1:]}), Y({line.split()[2][1:]}), Z({line.split()[3][1:]}))'
                print(new_line)
                print(new_line, file=output_file)
但是,将新行存储在内存中并在末尾写入可能更安全:

new_lines = []
with open("C:/Users/ZoharMa/Documents/G code/draft_of_g_code.txt", "r") as input_file:
    for line in input_file:
        line = line.strip()
        if "G1" in line:
            new_line = f"GoTo(X({line.split()[1][1:]}), Y({line.split()[2][1:]}), Z({line.split()[3][1:]}))"
            print(new_line)
            new_lines.append(new_line)

with open("copy.txt", "w") as output_file:
    for line in new_lines:
        print(line, file=output_file)

这样,如果您的程序失败,您就不会得到写了一半的文件。

非常感谢!成功了!你帮了我很多@不客气!请用投票箭头旁边的复选框图标将答案标记为已接受:)非常感谢!成功了!你帮了我很多@不客气!请用投票箭头旁边的复选框图标将答案标记为已接受:)Iv'e发现我需要在每行(GoTo命令)之前输入从00001、00002、00003开始的5位数字。。。。99999(作为转到行数)。例如:00001 GoTo(253),Y(174),Z(0.2))我可以用这个代码来做这件事吗?Iv'e发现我需要在每行(GoTo命令)前输入从00001,00002,00003开始的5位数字。。。。99999(作为转到行数)。例如:00001 GoTo(253),Y(174),Z(0.2))我可以用这个代码做这个吗?