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

from numpy import*
f = loadtxt("helium.dat")                                
data = f[:, 1]

for i in range(8):
  print data[i]

I used this code, but that's wrong because loadtxt supports only numbers.
Please help me

First time, I think that i can't do it by myself.
But luckly, I got it.

from numpy import*

x0 = []
for line in file('helium.dat'):
  line = line.lstrip('He')
  line_list = [float(x) for x in line.split()]
  x0.append(line_list)

for i in range(len(x0)):
  print array(x0[i]).item()

result:
1.31
1.32
1.39
1.41
1.38
1.39
1.21
1.45
2.31

I got more simple code, which gives same result.

from numpy import*
 
x0 = []
for line in file('helium.dat'):
   line = line.split()
   x = line[1]
   x0.append(x)

for i in range(len(x0)):
   print x0[i]

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...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.