![](/img/trans.png)
[英]Write a very basic copy from one file into another using command line arguments
[英]Copy/write line on to another file with minor change on the line
我正在尝试将文本写到新文件中,而没有将“位置12”处的文本更改为“位置” + newPosition +“位置12”。
这可能吗?
我尝试写到该行的末尾,但它写为新行,而不是在末尾连接
我的“线”是> Q952U6 | 在位置12 | 开始= 519,结束= 541 | 说明:E530K | LUSC Z52178 M2536nse_231ation T20A-18-3409 Q952U6 E530K
with open(filename) as f:
with open(fileoutput,'w') as fOut:
for line, line2 in itertools.izip_longest(f, f, fillvalue=''):
tokenizer=int(line.split()[4])
newPosition = get_new_indexs(tokenizer, str(line2))
fOut.write(line+' | new position ='+str(newPosition))
当您遍历文件时,每一行将在末尾包含换行符。 您必须将其剥离。 您可以这样做:
for line in file:
line = line.rstrip('\n')
或者,在您的特定情况下,您可以执行以下操作:
fOut.write(line.rstrip('\n') + ' | new position =' + str(newPosition))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.