Reputation: 33
I have a txt file that looks like this:
0.000E+00 0.000E+00 0.000E+00 0.000E+00
0.000E+00 0.000E+00 0.000E+00 0.000E+00
0.000E+00 0.000E+00 0.000E+00 0.000E+00
0.147E-01-0.158E-01-0.707E-02 0.173E-01
-0.593E-01 0.111E+00 0.153E+00 0.155E+00
0.859E-01-0.664E-01-0.281E+00-0.497E+00
-0.626E+00-0.587E+00-0.347E+00 0.560E-01
This is a very long file, so I can't do it manually. As you can see some of the numbers have spaces between them, and others (negative values) don't. When I'm trying to read it, I can't find a proper way to separate the values. If I choose delimiter='-', my script reads the values that have spaces between them as one value. If I choose delimiter=' ', my script does the same thing to the values saprated with minos. Is there a good wey to read the text and to be able to separate the values well?
my script:
inf=open("data","r")
for columns in ( raw.strip().split() for raw in inf ):
print (columns[0])
Upvotes: 0
Views: 261
Reputation: 502
I suggest using regex
for row in data:
print map(float, re.findall('-?\d\.\d{3}E[+-]\d{2}', row)
Upvotes: 0
Reputation: 1154
It's not the most optimal solution but try out this:
normalized_file_content = file_content.replace('-', ' -').replace('E -', 'E-')
This will add a space before the negative numbers but not between the E
and -
sign.
Upvotes: 7