简体   繁体   English

查找、替换和连接字符串

[英]Find, Replace, and Concatenate strings

I'm just re(learning) python after 5+ years of inactivity;经过 5 年以上的不活动,我只是重新(学习)python; it doesn't help that I wasn't great at it to begin with though… Here is what I'm trying to do:虽然我一开始并不擅长这并没有帮助......这就是我想要做的:

  1. find a sting in a file and replace it with another string (G00 with G1)在文件中找到一个字符串并将其替换为另一个字符串(G00 和 G1)
  2. find all of the instances where a line starts with a specific string and concatenate a string on it at the end查找行以特定字符串开头并在末尾连接字符串的所有实例
  3. The original file aaaa.gcode remains untouched and all of the changes are saved to bbbb.gcode.原始文件 aaaa.gcode 保持不变,所有更改都保存到 bbbb.gcode。

Individually both sets of code work as expected.两组代码分别按预期工作。 I've tried lots of different ways to add both sets of code together, but nothing works.我尝试了很多不同的方法将两组代码添加在一起,但没有任何效果。

Sample file (C:\gcode\aaaa.gcode)示例文件(C:\gcode\aaaa.gcode)

# start of file
some text
other text

# more text
G00 X1 Y1
G00 X2 Y2
# other text
G00 X3 Y3
G00 X4 Y4

#end of file

For the end result, I would like to have:对于最终结果,我想要:

# start of file
some text
other text

# more text
G1 X1 Y1 Z0.3
G1 X2 Y2 Z0.3
# other text
G1 X3 Y3 Z0.3
G1 X4 Y4 Z0.3

#end of file

Sample code / result示例代码/结果

  1. find replace - G00 with G1查找替换 - G00 与 G1
Code:
    with open(r'C:\gcode\aaaa.gcode', 'r') as fin, open(r'C:\gcode\bbbb.gcode', 'w') as fout:
    for line in fin:
        if line.startswith('G00'):
            fout.write(line[:-1] + ' Z0.3' + '\r')
        else:
            fout.write(line)
Result:
# start of file
some text
other text

# more text
G1 X1 Y1
G1 X2 Y2
# other text
G1 X3 Y3
G1 X4 Y4

#end of file
  1. Concatenate a string - add Z0.3 if line starts with G00连接字符串 - 如果行以 G00 开头,则添加 Z0.3
Code:
    with open(r'C:\gcode\aaaa.gcode', 'r') as fin, open(r'C:\gcode\bbbb.gcode', 'w') as fout:
        for line in fin:
            if line.startswith('G00'):
                fout.write(line[:-1] + ' Z0.3' + '\r')
            else:
                fout.write(line)
Result
# start of file
some text
other text

# more text
G00 X1 Y1 Z0.3
G00 X2 Y2 Z0.3
# other text
G00 X3 Y3 Z0.3
G00 X4 Y4 Z0.3

#end of file

I've tried various offshoots of the code below, but I always get the following error message:我已经尝试了下面代码的各种分支,但我总是收到以下错误消息:

with open(r'C:\gcode\aaaa.gcode', 'r') as fin, open(r'C:\gcode\bbbb.gcode', 'w') as fout:
    for line in fin:
        if line.startswith('G00'):
            fout.write(line[:-1] + ' Z0.3' + '\r')
            fout = fout.replace('G00', 'G1')   # newly added line
        else:
            fout.write(line)
AttributeError: '_io.TextIOWrapper' object has no attribute 'replace'

You are attempting doing .replace on file handle - this does not make sense, you should do it on line for example:您正在尝试在文件句柄上执行.replace - 这没有意义,您应该line执行,例如:

with open(r'C:\gcode\aaaa.gcode', 'r') as fin, open(r'C:\gcode\bbbb.gcode', 'w') as fout:
    for line in fin:
        line = line.replace('G00', 'G1')
        if line.startswith('G00'):
            fout.write(line[:-1] + ' Z0.3' + '\r')
        else:
            fout.write(line)

Note also that your code would work correctly if and only if lines are \r -delimited.另请注意,当且仅当行以\r分隔时,您的代码才能正常工作。

You can't replace from fout , you need to replace the content of the line.您不能从fout替换,您需要替换该行的内容。 It also doesn't make sense why you're setting out to be equal to fout.replace('G00', 'G1') # newly added line .你为什么要设置为等于fout.replace('G00', 'G1') # newly added line也是没有意义的。 Try this:尝试这个:

with open(r'C:\gcode\aaaa.gcode', 'r') as fin, open(r'C:\gcode\bbbb.gcode', 'w') as fout:
    for line in fin:
        if line.startswith('G00'):
            fout.write(line[:-1] + ' Z0.3' + '\r')
            new_line = line('G00', 'G1')   # newly added line
            # I believe you're trying to add this new line to the document
            fout.write(new_line)
        else:
            fout.write(line)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM