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

标签 python file-io types numpy genfromtxt

我有一个格式如下的文件:

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')])

关于python - 使用 numpy genfromtxt 从混合数据类型文件导入大整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18253450/

相关文章:

java - 如何在类定义中的泛型类型中捕获子类型?

Python Flask 应用程序在 TOTP 错误中前导零。 ( python 2.7)

python - itertools.repeat VS itertools.cycle

linux - 使用 perl 读取系统文件而不在打开时发出额外的搜索系统调用

c++ - 用 C++ 读取文本文件最优雅的方法是什么?

vb.net - 为什么使用 Type.Equals(t1, t2) 而不是相等运算符?

python - Tkinter gui 不工作 鼠标和键盘模块

python - 获取一行中最大元素的列,写回df

java - 多个线程可以同时将数据写入文件吗?

scala - 允许方法签名中存在子类型的特征