1

I have the following string of float numbers:

0.621464022829E+00-.143866495639E-020.266573765475E-02-.582189744480E-07

As you can see, there are no spaces between the numbers in this string. I am trying to make them csv. Therefore, I would like them to look like:

0.621464022829E+00,-.143866495639E-02,0.266573765475E-02,-.582189744480E-07

Is there a way to do this in python?. I tried to read the file using numpy, for example:

>>> w=numpy.loadtxt('coord', dtype='float')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/numpy/lib/npyio.py", line 856, in loadtxt
items = [conv(val) for (conv, val) in zip(converters, vals)]
ValueError: invalid literal for float():
0.621464022829E+00-.143866495639E-020.266573765475E-02-.582189744480E-07

but as there are no spaces, it is not possible. I also tried numpy.fromfile which seems to read the file, but the it shows this:

>>> w=numpy.fromfile('coord', dtype='float')
>>> w
array([  3.53728147e-057,   3.03226305e-100,   5.64351177e-038,
         3.70004839e-033,   1.24395502e-047,   3.37923148e-057,
         2.93531907e-086,   3.69971918e-057,   7.25394458e-043])

I would be very glad if somebody could shed some light in this problem.

Edited: The chosen answer works, but I would like to add that the solution proposed by @DSM is avery good as well:

np.genfromtxt("file.dat", delimiter=18) 
2
  • Who on earth produces output like this? Commented Jun 2, 2015 at 14:50
  • @LutzHorn I also thought the same... But it wasn't me at least! Commented Jun 2, 2015 at 14:51

1 Answer 1

4

Looks like you're stuck with fixed-width values instead of delimited. You'll have to slice up the string based on the character widths.

>>> s = '0.621464022829E+00-.143866495639E-020.266573765475E-02-.582189744480E-07'
>>> [float(s[i:i+18]) for i in range(0, len(s), 18)]
[0.621464022829, -0.00143866495639, 0.00266573765475, -5.8218974448e-08]

To read from a csv you could do something like

with open('file.csv') as f:
    data = [[float(line[i:i+18]) for i in range(0, len(line), 18)] for line in f.readlines()]

You could then pass this to numpy if you want

w = numpy.array(data)
Sign up to request clarification or add additional context in comments.

5 Comments

Note that np.genfromtxt also accepts widths in the delimiter argument, so np.genfromtxt("file.dat", delimiter=18) should work too.
@DSM I didn't know that, I like that solution better! If you post it as an answer you'll get a +1 from me :)
Look at this: 0.107388301057E+000.232744370788E-02-.629485908820E-030.972872524621E-03 Not possible with fixed-with approach! Or am I counting wrongly?
@muammar That string again seems to be fixed width, with each value being 18 characters wide.
I think you are right. I have just tested using the solution provided by @DSM and it seems to work well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.