Python - 从文件

时间:2015-04-24 11:40:43

标签: python

我需要有关此查询/问题的帮助......

如何从文件中的特定列收集数据,如下所示:

**[FILE.txt]**

CODE \t USERNAME \t SPENT \t COLUM1 \t COLUM2
CODE \t USERNAME \t SPENT \t COLUM1 \t COLUM2
CODE \t USERNAME \t SPENT \t COLUM1 \t COLUM2
CODE \t USERNAME \t SPENT \t COLUM1 \t COLUM2
CODE \t USERNAME \t SPENT \t COLUM1 \t COLUM2
CODE \t USERNAME \t SPENT \t COLUM1 \t COLUM2
CODE \t USERNAME \t SPENT \t COLUM1 \t COLUM2

请记住......这是此文件中用户的示例...每个用户在SPENT上都可以有不同的值......

我将把user1,user2,user3的值放在累加器中。

那么......我该怎么办?

已解决

lines = open("database").readlines()
quantidade = 0
for i in range(3):
    line = lines[i].split('\t')
    quantidade = quantidade + int(line[2])
print "Quantidade de Produtos Comprados: ",quantidade
#for line in range (10):
#   line = lines[-2].split() #Gets the second last value from the list and split on whitespace
#   print line

raw_input("Precione 'ENTER' para voltar ao menu principal... ")
return

0 个答案:

没有答案