reading a specific column from file

Hai Vu wuhrrr at
Thu Jan 17 10:47:20 CET 2008

Here is another suggestion:

col = 2 # third column
filename = '4columns.txt'
third_column = [line[:-1].split('\t')[col] for line in open(filename,

third_column now contains a list of items in the third column.

This solution is great for small files (up to a couple of thousand of
lines). For larger file, performance could be a problem, so you might
need a different solution.

More information about the Python-list mailing list