Reputation: 2520
I have a text file which I want to load into a NumPy
array with loadtext()
. The file is tab delimited and sometime I have a value after the last tab instead of empty:
Value1\tab\Value2\tab\value3\tab
Value4\tab\Value5\tab\value6\tab\value7
Value8\tab\Value9\tab\value10\tab
Value11\tab\Value12\tab\value13\tab
However, NumPy
gives me an error with that line:
ValueError: Wrong number of columns
Is it possible to load such a data structure directly into a NumPy
array (with None
as a value)? Or do I have to open the file, insert a None
if there is no value and load the manipulated text file in a NumPy
array?
Thanks
Upvotes: 1
Views: 438
Reputation: 19733
you can use np.genfromtxt
:
import numpy as np
my_array = np.genfromtxt("myfile.txt", delimiter="\t")
Upvotes: 0
Reputation: 69066
neither numpy.genfromtxt
or numpy.loadtxt
can deal with an uneven number of columns in a file. If you have access to pandas
, it can do what you need with pandas.read_table
.
import pandas as pd
df = pd.read_table('myfile.txt',header=None,sep='\t')
# to get the data in a numpy ndarray:
myarray = df.values
By default missing values are assigned NaN
, but you can change that with df.fillna(value)
Upvotes: 2