Sorting tsv into multiple lists


I need to be able to sort a 47x28 tsv file into a 47 different lists. So far I have:

f = open('EnergyConsumers.txt')
ln = f.readline()
ln.split('\t')
for i in ln:
    bel.append(i)

But the output is

['1', '9', '9', '0', '\t', '1', '9', '9', '1', '\t', '1', '9', '9', '2', 
'\t', '1', '9', '9', '3', '\t', '1', '9', '9', '4', '\t', '1', '9', '9', 
'5', '\t',]

I need it to read

[1990,1991,1992,1993]

Answer

first:

ln.split('\t')

does nothing. Result is lost unless assigned to something.

Then

for i in ln:
    bel.append(i)

iterates on the characters of the line.

you want:

bel.extend(ln.split())

(so it splits according to all spaces, including newlinen which is dropped)

with integer conversion:

bel.extend(map(int,ln.split()))

Finally, csv module is much easier to use

import csv
with open('EnergyConsumers.txt') as f:
    cr = csv.reader(f,delimiter="\t")
    bel.extend(map(int,next(cr)))

or

bel = [int(x) for x in next(cr)]

to create bel from scratch.

this reads one row, converts to integer, and adds the integers to bel