我想写一个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' –