Maqcel
Maqcel

Reputation: 509

Getting data from column in file

Welcome I manage to download all data from file but now I need only one column for the rest of my program I tried to do it like so but It didn't work as I intended it will.

with open("dane2.txt", "r") as fp:
    samples = [line.strip() for line in fp]
fp.close()
print("CONTENT LEN: %d" % samples.__len__())
for i in range(0,samples.__len__()):
    print(samples[i][0]) #1 column

The output I got was:

CONTENT LEN: 990
2
2
to 990

So I figured that this list is getting all string values I tried to use:

[float(i) for i in samples]

But it didn't work either so here I'm!

Data in my file looks like this:

241.32  241.08  241.72
241.9   241.18  242.11
241.62  241.2   241.83
241.85  241.46  242.02
241.56  241.15  241.8
241.29  240.85  241.54

Upvotes: 0

Views: 35

Answers (1)

bashBedlam
bashBedlam

Reputation: 1500

I think that you also need to split (' ') your samples at the spaces. Here's the code:

with open("dane2.txt", "r") as fp:
    samples = [line.strip ().split ('  ') for line in fp]
fp.close()
for i in range (len (samples)) : 
    print (samples [i] [0])

Or if you want to see all of you data use this one :

with open("dane2.txt", "r") as fp:
    samples = [line.strip ().split ('  ') for line in fp]
    print (samples)
fp.close()
for i in range (len (samples)) : 
    print (samples [i] [0], end = '  ')
    print (samples [i] [1], end = '  ')
    print (samples [i] [2])

And here's a screenshot of my output :

Screenshot

Upvotes: 1

Related Questions