我有一個文件,有些行在錯誤的位置有\ n。我能夠正確地找到它們,但是當我嘗試將我的發現輸出到一個新文件時,即使打印結果,它們仍會顯示\ 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
'w.write(line)'應該是'w.write(outputline)'我想。 – utdemir 2013-02-19 19:48:12
沒有一個是正確的,因爲如果該行不需要任何修復。 – rjbogz 2013-02-19 19:49:30
有沒有什麼有趣的與隱藏字符在那裏?像某個地方的「\ r」一樣? – Hoopdady 2013-02-19 19:55:06