reading column data

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2007
Posts: 6
Reputation: random7 is an unknown quantity at this point 
Solved Threads: 0
random7 random7 is offline Offline
Newbie Poster

reading column data

 
0
  #1
Apr 17th, 2008
Hi, everyone

I have a question about reading column data from file.

helium.dat
He 1.31
He 1.32
He 1.39
He 1.41
He 1.38
He 1.39
He 1.21
He 1.45
He 2.31

I want to print just the 2nd column data(number) from helium.dat like below
1.31
1.32
1.39
1.41
1.38
1.39
1.21
1.45
2.31

  1. from numpy import*
  2. f = loadtxt("helium.dat")
  3. data = f[:, 1]
  4.  
  5. for i in range(8):
  6. print data[i]

I used this code, but that's wrong because loadtxt supports only numbers.
Please help me
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 6
Reputation: random7 is an unknown quantity at this point 
Solved Threads: 0
random7 random7 is offline Offline
Newbie Poster

Re: reading column data

 
0
  #2
Apr 18th, 2008
First time, I think that i can't do it by myself.
But luckly, I got it.

  1. from numpy import*
  2.  
  3. x0 = []
  4. for line in file('helium.dat'):
  5. line = line.lstrip('He')
  6. line_list = [float(x) for x in line.split()]
  7. x0.append(line_list)
  8.  
  9. for i in range(len(x0)):
  10. print array(x0[i]).item()

result:
1.31
1.32
1.39
1.41
1.38
1.39
1.21
1.45
2.31
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 6
Reputation: random7 is an unknown quantity at this point 
Solved Threads: 0
random7 random7 is offline Offline
Newbie Poster

Re: reading column data

 
0
  #3
Apr 18th, 2008
I got more simple code, which gives same result.

  1. from numpy import*
  2.  
  3. x0 = []
  4. for line in file('helium.dat'):
  5. line = line.split()
  6. x = line[1]
  7. x0.append(x)
  8.  
  9. for i in range(len(x0)):
  10. print x0[i]
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 2
Reputation: hardik.38 is an unknown quantity at this point 
Solved Threads: 0
hardik.38 hardik.38 is offline Offline
Newbie Poster

Re: reading column data..no need of additional code u people write

 
0
  #4
Sep 2nd, 2008
def main():
x0=[]
for line in file('C:\h1.txt'):
line = line.split()
x = line[1]
print x

Hi...Guys..i just join community last night...and I think u have done a great job...but i thnk the code u write is bit long..we can make it short bye just like i have done above..

thanx..hardik here...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 2843 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC