我正在尝试将多个内容写入txt文件,但由于某种原因,我无法让每个条目最终都在新行上。我已经搞砸了放置' \ n'在不同的地方,但结果仍然是一样的。以下是使用的代码:
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.
我觉得我可能在这里犯了一个菜鸟错误,但有没有一种简单的方法可以让文件将每个条目写入新行?提前感谢您的任何帮助。
答案 0 :(得分:2)
您可以使用print()
功能重定向到文件。
此外,使用PEP 8 — the Style Guide for Python Code打开文件是一种很好的做法:无需担心拨打close()
:
from collections import Counter
with open('path1', 'r') as file_1:
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:
print('{:3} ==> {:15}'.format('Word','Count'), file=file_2)
print('-' * 18, file=file_2)
for word, occurrence in wordCounter.most_common():
print('{:3} ==> {:15}'.format(word,occurrence), file=file_2)
我还建议您遵循https://www.cloudera.com/documentation/spark2/latest/topics/spark2_known_issues.html#ki_spark_on_hbase,其中解释了命名约定。
注意:要在Python 2中使用print()
函数,您可以使用脚本顶部的__future__
指令。
from __future__ import print_function
答案 1 :(得分:1)
正如我所提到的,你使用backslah(\
),而不是正斜杠(/
)。
这是您的固定代码:
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}\n'.format('Word','Count'))
File_2.write(str('-' * 18)+'\n')
for (word,occurrence) in wordCounter.most_common():
File_2.write('{:3} ==> {:15}\n'.format(word,occurrence))
File_1.close()
你也不需要为File2添加一个关闭,因为你可以为你做