Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading specific columns from a text file in python

I have a text file which contains a table comprised of numbers e.g:

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 contained only in the second column, how do i extract that column into a list?

like image 599
Jethro Avatar asked May 13 '15 13:05

Jethro


People also ask

How do you read a specific row in a text file in Python?

Use readlines() to Read the range of line from the File The readlines() method reads all lines from a file and stores it in a list. You can use an index number as a line number to extract a set of lines from it. This is the most straightforward way to read a specific line from a file in Python.

How to read text files in Python?

Summary: in this tutorial, you learn various ways to read text files in Python. The following shows how to read all texts from the readme.txt file into a string: First, open a text file for reading by using the open () function. Second, read text from the text file using the file read (), readline (), or readlines () method of the file object.

How do you write in a specific line and column using Python?

How do you write in a specific line and column in a text file using Python? You will have to count the number of lines that the text file has and work out how many you need. You will also need to identify how to create a new line in the file when you need one.

How to read the content of a specific CSV file in Python?

Python read the content of a specific csv file column : Python provides csv module to do read-write operations on a csv file. We can use this module to read the contents line by line or with a slight change, we can read the contents of a specific column. Let’s consider the below csv file :

How to read 10th line from a file in Python?

1 # open the sample file used file = open('test.txt') 2 # read the content of the file opened content = file.readlines () 3 # read 10th line from the file print("tenth line") print(content [9]) 4 # print first 3 lines of file print("first three lines") print(content [0:3]) Output tenth line. This is line 10. This is line 1.This is line 2.This ...


3 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 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.

like image 69
ForceBru Avatar answered Oct 04 '22 16:10

ForceBru


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.]) 
like image 23
aerobiomat Avatar answered Oct 04 '22 15:10

aerobiomat


You have a space delimited file, so use the module designed for reading delimited values files, 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) 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]
like image 40
Adam Smith Avatar answered Oct 04 '22 15:10

Adam Smith