1

I need help with this query/question...

How can i collect data from specific column in file, like this:

**[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

Remember...This is examples of users in this files... Every user can have diferent values on SPENT...

I'll put the values of user1,user2,user3 in a accumulator.

So...how can i proceed?

SOLVED WITH

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
Tales Valente
  • 23
  • 1
  • 7
  • Sorry but, i dont found any like this... – Tales Valente Apr 24 '15 at 12:00
  • Use for loop with split method. – jester112358 Apr 24 '15 at 12:00
  • Dont Working... =/ lines = open("database").readlines() for line in range (10): line = lines[-2].split() #Gets the second last value from the list and split on whitespace print line – Tales Valente Apr 24 '15 at 12:04
  • try this method which iterates over the file: colnum = 2 # spent with open('file.txt') as f: for line in f: do_something(line.split('\t')[colnum]) – toudi Apr 24 '15 at 12:05
  • I guarantee the split works here. Try to work it out! – jester112358 Apr 24 '15 at 12:09
  • hint: for lines in infile: lines = lines.split("\t") After this you can call the SPENT column by lines[2] – jester112358 Apr 24 '15 at 12:11
  • Solved with 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 – Tales Valente Apr 24 '15 at 12:13

0 Answers0