Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read specific column from .dat-file in Python

Tags:

python

file-io

I have a results.dat file with some data like this:

7522126 0   0   0   0   0   0   -419.795    -186.24 1852.86 0.134695    -0.995462   -2.53153
7825452 0   0   0   0   0   0   -419.795    -186.24 1852.86 0.134695    -0.995462   -2.53153
8073799 0   0   0   0   0   0   -345.551    -140.711    1819.04 -0.0220266  -0.85992    -2.29598

The values are each separated by a tab.

I want to extract the value in e.g the 8th column for every single line, and save it to an array. So the output should be this:

-419.795
-419.795
-345.551

What's the easiest way to accomplish this?

like image 877
Boxiom Avatar asked Mar 29 '15 11:03

Boxiom


4 Answers

with open('results.dat') as f:
    [line.split()[7] for line in f]  

or define a function,

get_col = lambda col: (line.split('\t')[col-1] for line in open('results.dat'))  

Now call the function with desired column number. get_col(8) gives 8th column data. To store it in array,

array.array('d',map(float,get_col(8)))
like image 192
Nizam Mohamed Avatar answered Oct 23 '22 02:10

Nizam Mohamed


You could use csv module.

import csv
with open('file') as f:
    reader = csv.reader(f, delimiter="\t")
    for line in reader:
        print(line[7])
like image 44
Avinash Raj Avatar answered Oct 23 '22 04:10

Avinash Raj


first of all read the file (result.dat) file in a file object

file = open('result.dat')

now create an empty list

lst = []

loop through each line of the file

for line in file:
    lst += [line.split()]

now lst is a list of list , where each inner list is a instance (row ) of the result.dat

now you can extract any column (in your case it is 8th) apply list comprehension for this

column8 = [x[7] for x in lst]

hope this helps,

like image 4
lazarus Avatar answered Oct 23 '22 03:10

lazarus


What's the easiest way to accomplish this?

Would recommend numpy.genfromtxt if the other answers don't suit your needs.

import numpy
data = numpy.genfromtxt('result.dat', delimiter='\t')
print data[:,7]
like image 3
Matthew Avatar answered Oct 23 '22 03:10

Matthew