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?
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,
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With