Read Specific Column From. Dat-File in Python

Read Specific Column From. Dat-File in Python

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?

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)))

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])

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,

1

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]
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.