我有一个文件,有些行在错误的位置有一个\n。我能够正确地找到它们,但是当我尝试将我的发现输出到新文件时,它们仍然显示\n结果,即使我打印了结果,结果也很好。以下是我目前的代码:
f = open("DUP1.txt","r")
w = open("output.txt", "w")
mark = 0
for line in f:
if mark == 1:
mark = 0
w.write(outputline.replace("\n","\t") + line)
else:
subp = line.find(".")
if subp < 8:
mark = 1
outputline = line.replace("\n","")
else:
w.write(line)
我打开的文件如下所示:
ABC0005 other info here
ABC0005.23
other info here
ABC0005.46
other info here
我想让它看起来像:
ABC0005 other info here
ABC0005.23 other info here
ABC0005.46 other info here
即使我搞不清楚是怎么回事,这里有一句漂亮的台词:
编辑:John Clements's answer更好。你知道吗
这条线:
当
"."
不在subp
时返回-1
。这打乱了你的逻辑。你知道吗相关问题 更多 >
编程相关推荐