Reading specific columns from a text file in python Reading specific columns from a text file in python python python

Reading specific columns from a text file in python


f=open(file,"r")lines=f.readlines()result=[]for x in lines:    result.append(x.split(' ')[1])f.close()

You can do the same using a list comprehension

print([x.split(' ')[1] for x in open(file).readlines()])

Docs on split()

string.split(s[, sep[, maxsplit]])

Return a list of the words of the string s. If the optional second argument sep is absent or None, the words are separated by arbitrary strings of whitespace characters (space, tab, newline, return, formfeed). If the second argument sep is present and not None, it specifies a string to be used as the word separator. The returned list will then have one more item than the number of non-overlapping occurrences of the separator in the string.

So, you can omit the space I used and do just x.split() but this will also remove tabs and newlines, be aware of that.


You have a space delimited file, so use the module designed for reading delimited values files, csv.

import csvwith open('path/to/file.txt') as inf:    reader = csv.reader(inf, delimiter=" ")    second_col = list(zip(*reader))[1]    # In Python2, you can omit the `list(...)` cast

The zip(*iterable) pattern is useful for converting rows to columns or vice versa. If you're reading a file row-wise...

>>> testdata = [[1, 2, 3],                [4, 5, 6],                [7, 8, 9]]>>> for line in testdata:...     print(line)[1, 2, 3][4, 5, 6][7, 8, 9]

...but need columns, you can pass each row to the zip function

>>> testdata_columns = zip(*testdata)# this is equivalent to zip([1,2,3], [4,5,6], [7,8,9])>>> for line in testdata_columns:...     print(line)[1, 4, 7][2, 5, 8][3, 6, 9]


I know this is an old question, but nobody mentioned that when your data looks like an array, numpy's loadtxt comes in handy:

>>> import numpy as np>>> np.loadtxt("myfile.txt")[:, 1]array([10., 20., 30., 40., 23., 13.])