Jython: read array from a text file - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Jython: read array from a text file

sissigongsissigong Posts: 1Member
Hi, I have a question about reading float data from a text file and hope somebody can give me some ideas, code is strongly appreciated!

I wrote some float numbers in a text file as below:
0.1550000 , 863500.0 , 344600.0 , 1.5999969E-02 ,
0.1600000 , 863500.0 , 344600.0 , 3.1999987E-02 ,
0.1650000 , 863500.0 , 344600.0 , 4.7999956E-02 ,
0.1700000 , 863500.0 , 344600.0 , 6.3999951E-02 ,
....

Now I want to read these numbers from this text file into an array to use later in the code. I hope the array will look like:
array=[0.1550000,863500.0,344600.0,1.5999969E-02,0.1600000,863500.0,344600.0, 3.1999987E-02, .....]

My CODE is:
def main(args):

inf=open("a.txt", 'r')

z = []
z = inf.readlines()
print z

result: z is a big string, not individual float numbers
--------------------------------------------
If I change my code to:
def main(args):

inf=open("a.txt", 'r')

z = []
for line in inf.readlines():
for number in line.strip('
'):
z.append(number)
print z

result is like: [------ '2', '.', '9', '5', '0', '0', '0', '0', ' ', ' ', ' ', ' ', ' ', ',', ' ', ' ', ' ', '8', '9', '2', '0', '5', '0', '.', '0', ' ', ' ', '-------]

Please help me... Thanks in advance! It's really hard to google good help of Jython!

Cheers!



Sign In or Register to comment.