reading a specific column from file

John Machin sjmachin at lexicon.net
Thu Jan 17 12:28:58 CET 2008


On Jan 17, 8:47 pm, Hai Vu <wuh... at gmail.com> wrote:
> Here is another suggestion:
>
> col = 2 # third column
> filename = '4columns.txt'
> third_column = [line[:-1].split('\t')[col] for line in open(filename,
> 'r')]
>
> 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.

Using the maxsplit arg could speed it up a little:

line[:-1].split('\t', col+1)[col]




More information about the Python-list mailing list