OiO.lk Blog python Fixing badly formatted floats with numpy
python

Fixing badly formatted floats with numpy


I am reading a text file only containing floating point numbers using numpy.loadtxt. However, some of the data is corrupted and reads something like X.XXXXXXX+YYY instead of X.XXXXXXXE+YY (Missing E char). I’d like to interpret them as the intended floating point number (or NaN if impossible) and wondered if there was any easy way to do this upon reading the file instead of manually correcting each entries in the file since it contains hundreds of thousands of lines of data.

MWE:

import numpy as np

data = np.loadtxt("path/to/datafile")

Example of error raised:

ValueError: could not convert string '0.710084093014+195' to float64 at row 862190, column 6



You need to sign in to view this answers

Exit mobile version