[英]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:
虽然我一开始并不擅长这并没有帮助......这就是我想要做的:
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示例代码/结果
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
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.