【发布时间】:2015-11-06 17:26:18
【问题描述】:
我有一个如下所示的数据文件:
TOPIC:topic_0 2056
ab 2.0
cd 5.0
ef 3.0
gh 10.0
TOPIC:topic_1 1000
aa 3.0
bd 5.0
gh 2.0
等等……直到 TOPIC:topic_2000。第一行是主题和权重。也就是说,我有该特定主题中的单词及其各自的权重。
现在,我想总结每个主题的第二列并检查它给出的价值。也就是说,我想得到如下输出:
Topic:topic_0 20
Topic:topic_1 10
即主题号与列值之和(即主题1中,词的权重为2,5,3,10)。我尝试使用:
with open('Input.txt') as in_file:
for line in in_file:
columns = line.split(' ')
value = columns[0]
if value[:6] == 'TOPIC:':
total_value = columns[1]
total_value = total_value[:-1]
total_values = float(total_value)
#print '\n'
print columns[0]
但是,我不知道如何从这个开始。这只是打印主题编号。请帮忙!
【问题讨论】:
标签: python linux python-2.7