找到你要的答案

Q:Numpy loadtext()

Q:loadtext() NumPy

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

Numpy gives me "ValueError: Wrong number of columns" with that line. 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

我有一个文本文件,我想装一个与loadtext() NumPy数组。该文件是制表符分隔,有时我有一个值后,最后一个标签,而不是空:

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

NumPy给我“ValueError:这一行列的“打错了。它是可能的负荷等数据结构直接进入NumPy数组(有没有价值)或者我要打开文件,插入一个没有价值和在NumPy数组操作的文本文件加载?

谢谢

answer1: 回答1:

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)

不numpy.genfromtxt或numpy.loadtxt可以处理文件中的列的数目不均衡。如果你有机会到大熊猫,它可以根据你的需要和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

默认情况下,缺失值分配给楠,但你可以改变fillna DF(价值)。

answer2: 回答2:

you can use np.genfromtxt:

import numpy as np
my_array = np.genfromtxt("myfile.txt", delimiter="\t")

你可以使用np.genfromtxt:

import numpy as np
my_array = np.genfromtxt("myfile.txt", delimiter="\t")
python  arrays  numpy