我有一个充满句子的文件,我想用它做一个单字组:
这是我的代码,只收到一个字母,我希望它能得到这个词
old_lines = open("f.final",'r').readlines()
new_lines = []
for line in old_lines:
words = line.split()
new_lines.append(words)
print new_lines
for lines in new_lines:
c = Counter(str(lines))
with open("final.final", 'w') as f:
for k,v in c.items():
f.write("{} {}\n".format(k,v))
答案 0 :(得分:1)
您正在从字符串(即str(lines)
)构建计数器,该字符串会对字符串中的每个字符进行计数。您应该直接从列表中构建计数器。这应该针对所有行进行,而不仅仅是最后一行:
with open("f.final") as f, open("final.final", 'w') as out_f:
# take count of all words from all lines
c = Counter(word for line in f for word in line.strip().split())
# write to output file
for k, v in c.items():
out_f.write("{} {}\n".format(k,v))