Python

Moderators: None (Apply to moderate this forum)
Number of threads: 473
Number of posts: 1172

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Jython: read array from a text file Posted by sissigong on 20 Jul 2010 at 2:47 PM
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('\n'):
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!






 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.