我有一个文本文件,其中包含以空格“”分隔的字符串值。我需要将每个字符串值设置为等于某个数字。 到目前为止,我已经从文件读取到字典,其中每个唯一键对应一个值编号。
import collections
with open('test.txt', 'r') as f:
c = collections.Counter(f.read().split())
newvalue = 0
for key, value in c.iteritems():
newvalue +=1
c[key] = newvalue
print key, newvalue
我现在的问题是我不知道如何在另一个文件中编写输出,保留这个结构:
我的文字档案:
所需的输出文件:
有人可以帮帮我吗?
答案 0 :(得分:0)
一些问题:
Counter
个对象没有iteritems
方法。这是一个可以满足您需求的工作示例。最大的变化是使用嵌套列表来保留输入文件的布局。
import collections
with open('test.txt', 'r') as f: # open the file
lines = [] # initialize a list
for line in f: # for each line in the file,
# add a list of that line's words to the initialized list
lines.append(line.split())
# save a Counter out of the concatenation of those nested lists
c = collections.Counter(sum(lines, []))
with open('testout.txt', 'w') as output: # start a file
for line in lines: # for each list in the list we made,
result = [] # initialize a list
for word in line: # for each word in the nested list,
# add that word's count to the initialized list
result.append(str(c[word]))
# write a string representation of the list to the file
output.write(' '.join(result) + '\n')