Skip to main content
 首页 » 编程设计

python之使用 numpy genfromtxt 从混合数据类型文件导入大整数

2024年11月01日12davidwang456

我有一个格式的文件:

1    2.5264    24106644528  astring 

我想导入数据。我正在使用:

>>> numpy.genfromtxt('myfile.dat',dtype=None) 
 
Traceback (most recent call last): 
  File "<pyshell#4>", line 1, in <module> 
    numpy.genfromtxt('myfile.dat',skip_header=27,dtype=None) 
  File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 1691, in genfromtxt 
    output = np.array(data, dtype=ddtype) 
OverflowError: Python int too large to convert to C long 

我检查了我(32 位)系统上的最大整数:

>>> import sys 
>>> sys.maxint 
2147483647 

有没有办法增加整数限制?或者我可以用另一种方式解决我的导入问题(不在文件中的所有整数之后放置“.0”)吗?

请您参考如下方法:

意识到我可以做到:

>>> numpy.genfromtxt('myfile.dat',dtype=['i4','f8','f8','a14']) 
 
array((1, 2.5264, 24106644528.0, 'astring'),  
  dtype=[('f0', '<i4'), ('f1', '<f8'), ('f2', '<f8'), ('f3', 'S14')])