原文:https://docs.scipy.org/doc/numpy/reference/generated/numpy.genfromtxt.html
校对:(虚位以待)
numpy.
genfromtxt
(fname, dtype=<type 'float'>, comments='#', delimiter=None, skip_header=0, skip_footer=0, converters=None, missing_values=None, filling_values=None, usecols=None, names=None, excludelist=None, deletechars=None, replace_space='_', autostrip=False, case_sensitive=True, defaultfmt='f%i', unpack=None, usemask=False, loose=True, invalid_raise=True, max_rows=None)[source]从文本文件加载数据,缺少值按指定处理。
第一个skip_header行之后的每一行在分隔符字符处分割,且注释字符后面的字符将被丢弃。
参数: | fname:file,str,str,generator的列表
dtype:dtype,可选
注释:str,可选
分隔符:str,int或sequence,可选
skiprows:int,可选
skip_header:int,可选
skip_footer:int,可选
转换器:变量,可选
缺少:变量,可选
missing_values:变量,可选
filling_values:变量,可选
usecols:sequence,可选
names:{None,True,str,sequence},可选
excludelist:sequence,可选
deletechars:str,可选
defaultfmt:str,可选
自动分页:bool,可选
replace_space:char,可选
case_sensitive:{True,False,'upper','lower'},可选
解包:bool,可选
usemask:bool,可选
松散:bool,可选
invalid_raise:bool,可选
max_rows:int,可选
|
---|---|
返回: | out:ndarray
|
也可以看看
numpy.loadtxt
笔记
参考文献
[R20] | Numpy用户指南,I / O with Numpy部分。 |
例子
>>> from io import StringIO
>>> import numpy as np
带有混合dtype的逗号分隔文件
>>> s = StringIO("1,1.3,abcde")
>>> data = np.genfromtxt(s, dtype=[('myint','i8'),('myfloat','f8'),
... ('mystring','S5')], delimiter=",")
>>> data
array((1, 1.3, 'abcde'),
dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')])
使用dtype =无
>>> s.seek(0) # needed for StringIO example only
>>> data = np.genfromtxt(s, dtype=None,
... names = ['myint','myfloat','mystring'], delimiter=",")
>>> data
array((1, 1.3, 'abcde'),
dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')])
指定dtype和名称
>>> s.seek(0)
>>> data = np.genfromtxt(s, dtype="i8,f8,S5",
... names=['myint','myfloat','mystring'], delimiter=",")
>>> data
array((1, 1.3, 'abcde'),
dtype=[('myint', '<i8'), ('myfloat', '<f8'), ('mystring', '|S5')])
具有固定宽度列的示例
>>> s = StringIO("11.3abcde")
>>> data = np.genfromtxt(s, dtype=None, names=['intvar','fltvar','strvar'],
... delimiter=[1,3,5])
>>> data
array((1, 1.3, 'abcde'),
dtype=[('intvar', '<i8'), ('fltvar', '<f8'), ('strvar', '|S5')])