Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

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.