reading a column from a file

G

Gary Wessle

Hi

I have a file with data like
location pressure temp
str floot floot

I need to read pressure and temp in 2 different variables so that I
can plot them as lines. is there a package which reads from file with
a given formate and returns desired variables? or I need to open,
while not EOF read, parse, build list, return?

thanks
 
P

pyGuy

f = open("datafile.txt", "r")
data = [line.split('\t') for line in f]
f.close()
pressure = [float(d[1]) for d in data]
temp = [float(d[2]) for d in data]
---------------------------------------------------

This will parse the file into a matrix stored in 'data'. The last two
lines simply iterate through second and third columns respectively,
converting each element to a float (from string as it was read in from
file) and assign to the appropriate vars.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,213
Latest member
DonnellTol

Latest Threads

Top