3

我想阅读一个简单的表格,使用 astropy.table。该行的第一个元素是一个大整数。它失败,并显示错误消息:“OverflowError: Python int too large to convert to C long”。我怎样才能避免这种情况?

细节:

该表在 test.cat 中。很简单,一行:81421100001 2 1 1 37.5991 1.0213 785.364 539.291

这是我使用的代码:

import numpy as np
from astropy.table import Table

catalog_filename = 'test.cat'

t = Table.read(catalog_filename, format='ascii')

我收到以下错误:

Traceback (most recent call last):
  File "catread.py", line 15, in <module>
    t = Table.read(catalog_filename, format='ascii')
  File "/usr/local/lib/python2.7/dist-packages/astropy/table/table.py", line 2561, in read
    return io_registry.read(cls, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/astropy/io/registry.py", line 319, in read
    table = reader(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/astropy/io/ascii/connect.py", line 18, in read_asciitable
    return read(filename, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/astropy/io/ascii/ui.py", line 154, in read
    dat = _guess(table, new_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/astropy/io/ascii/ui.py", line 196, in _guess
    dat = reader.read(table)
  File "/usr/local/lib/python2.7/dist-packages/astropy/io/ascii/core.py", line 872, in read
    table = self.outputter(cols, self.meta)
  File "/usr/local/lib/python2.7/dist-packages/astropy/io/ascii/core.py", line 670, in __call__
    self._convert_vals(cols)
  File "/usr/local/lib/python2.7/dist-packages/astropy/io/ascii/core.py", line 652, in _convert_vals
    col.data = converter_func(col.str_vals)
  File "/usr/local/lib/python2.7/dist-packages/astropy/io/ascii/core.py", line 611, in converter
    return numpy.array(vals, numpy_type)
OverflowError: Python int too large to convert to C long
4

1 回答 1

1

如上所述,这现在是一个 astropy 问题 ( https://github.com/astropy/astropy/issues/2234 ),并且有一个建议的修复程序,它会在溢出的情况下自动回退到字符串。同时,您可以指示ascii.read函数使用特定的 numpy dtype 将列从文本字符串转换为最终表格列。使用converters关键字 arg 如下所示。

>>> ascii.read(['8142110000100000000 1 2 3'], 
               converters={'col1': [ascii.convert_numpy(np.int64)]})
<Table rows=1 names=('col1','col2','col3','col4')>
array([(8142110000100000000, 1, 2, 3)], 
    dtype=[('col1', '<i8'), ('col2', '<i8'), ('col3', '<i8'), ('col4', '<i8')])

>>> ascii.read(['8142110000100000000 1 2 3'], 
                converters={'col1': [ascii.convert_numpy(np.float)]})
<Table rows=1 names=('col1','col2','col3','col4')>
array([(8.1421100001e+18, 1, 2, 3)], 
  dtype=[('col1', '<f8'), ('col2', '<i8'), ('col3', '<i8'), ('col4', '<i8')])
于 2014-03-25T18:59:25.193 回答