Reading specific columns from a text file in python

I have a text file that contains a table consisting of numbers, for example:

5 10 6

6 20 1

7 30 4

8 40 3

9 23 1

4 13 6

if, for example, I want the numbers to be contained only in the second column, how can I extract this column to the list?

+7
python list text-files
source share
4 answers
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 list comprehension

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

Documents for split()

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

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

This way you can omit the space that I used and make only x.split() , but this will also remove the tabs and x.split() . Remember this.

+10
source share

You have a space delimited file, so use a module designed to read value delimited csv , csv .

 import csv with 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) is useful for converting rows to columns or vice versa. If you read the file line by line ...

 >>> 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] 
+6
source share

You can use the zip function with a list:

 with open('ex.txt') as f: print zip(*[line.split() for line in f])[1] 

result:

 ('10', '20', '30', '40', '23', '13') 
+4
source share

First of all, we open the file and datafile , then use the .read() method, read the contents of the file, and then separate the data that returns something like: ['5', '10', '6', '6', '20', '1', '7', '30', '4', '8', '40', '3', '9', '23', '1', '4', '13', '6'] , and we applied the list of cuts in this list, start with the element at index position 1 and skip the next 3 elements until it reaches the end of the loop.

 with open("sample.txt", "r") as datafile: print datafile.read().split()[1::3] 

Output:

 ['10', '20', '30', '40', '23', '13'] 
+1
source share

All Articles