我想寫一個txt文件的多個東西,但由於某種原因,我不能讓每個條目結束了一個新的行。我在不同的地方放置了'\ n',但結果仍然相同。下面是正在使用的代碼:Python 3.X寫入文本文件不會創建新行
from collections import Counter
File_1 = open('path1', 'r')
wordCounter = Counter(File_1.read().lower().replace('<p>','').replace('<p><b>','').replace('</p>','').replace('</b>','').replace('.','').replace("'",'').replace('"','').replace('<i>','').replace('</i>','').replace(',','').replace('(','').replace('-','').replace(')','').replace('<b>','').replace(';','').split())
with open('path2','w') as File_2:
File_2.write('{:3} ==> {:15}'.format('Word','Count'))
File_2.write('-' * 18)
for (word,occurrence) in wordCounter.most_common():
File_2.write('{:3} ==> {:15}'.format(word,occurrence))
File_1.close()
File_2.close()
試圖忽略了許多替代電話,我與需要清洗,纔可以進行排序一個靠不住的文本文件的工作。我想每個條目出現像這樣:
Word ==> Count
Eighteen dashes
Entry 1 ==> # of entries
Entry 2 ==> # of entries
etc.
我到底是什麼了得到的是:
Word ==> Count ------------------Entry 1 ==> # of entriesEntry 2 ==> # of entries, etc.
我覺得我可能在這裏做一個新手的錯誤,但有一個簡單的方法來將每個條目寫入新行的文件?預先感謝您的幫助。
你做錯了。它是'\ n',而不是'/ n' –