'Read in scientific notation with numpy loadtxt

I have a datafile containing 3 columns with floats in scientific notation,

20346.7353    0.139945D+04    0.922215D+03
20347.0689    0.139958D+04    0.922340D+03
20347.4024    0.139970D+04    0.922464D+03
20347.7360    0.139983D+04    0.922589D+03
...           ...             ...

, which I want to read using a python script in order to plot the data with pyplot. However the numpy loadtxt command

x, y1, y2 = np.loadtxt(sys.argv[1], unpack=True, dtype=float, usecols=(0, 1, 2))

does not recognise the floats due to the scientific notation. How could I read in this file (with approximately 30000 lines)? Thank you



Solution 1:[1]

The python scientific notation uses E, not D. So you can replace them before reading the data:

# reading from string here, read from file in your case
data = '''20346.7353    0.139945D+04    0.922215D+03
20347.0689    0.139958D+04    0.922340D+03
20347.4024    0.139970D+04    0.922464D+03
20347.7360    0.139983D+04    0.922589D+03'''

import io

np.loadtxt(io.StringIO(data.replace('D', 'E')))

output:

array([[20346.7353,  1399.45  ,   922.215 ],
       [20347.0689,  1399.58  ,   922.34  ],
       [20347.4024,  1399.7   ,   922.464 ],
       [20347.736 ,  1399.83  ,   922.589 ]])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mozway