Alternatively, you can simply call numpy.genfromtxt and use missing_values kwarg.
eg. with this data saved as data.txt :
1 0.2 0.3 1.#INF00 2 0.5 0.6 0.7 3 1.#IND00 0.1 0.2 4 0.4 0.4 0.5 5 0.5 0.5 0.7
You can just do something like this (we need to set the comment identifier to something other than the standard "#" in this particular case):
import numpy as np data = np.genfromtxt('data.txt', missing_values=['1.#INF00', '1.#IND00'], comments='somethingelse')
This gives:
array([[ 1. , 0.2, 0.3, nan], [ 2. , 0.5, 0.6, 0.7], [ 3. , nan, 0.1, 0.2], [ 4. , 0.4, 0.4, 0.5], [ 5. , 0.5, 0.5, 0.7]])
source share