470,821 Members | 2,077 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,821 developers. It's quick & easy.

reading a column from a file

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
May 8 '06 #1
3 1378
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.

May 8 '06 #2
Check out the csv module.

-Larry Bates

Gary Wessle wrote:
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

May 8 '06 #3
Check out the csv module.

-Larry Bates

Gary Wessle wrote:
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

May 8 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by jamait | last post: by
3 posts views Thread by Sobhan | last post: by
5 posts views Thread by Scott M. Lyon | last post: by
4 posts views Thread by Amit Maheshwari | last post: by
9 posts views Thread by dba123 | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.