InconsistentTableError rows different from columns error of astropy Table.read()

InconsistentTableError rows different from columns error of astropy Table.read()

我正在尝试读取带有 astropy 的 Table.read().txt 文件:

data = Table.read('BD_FONDECYT_1120715Py(03-2019).txt', format='ascii', guess=False)

我添加了 guess=False,因为没有它,错误就不太清楚了。

我正在使用数据库,并且总是使用脚本来读取它。现在,我添加了一些新数据,代码显示了这个错误:

'Traceback (most recent call last):
data = Table.read('BD_FONDECYT_1120715Py(03-2019).txt', format='ascii', guess=False)                                                                  
File "C:\Python3\lib\site-packages\astropy\table\table.py", line 2550, in read 
out = io_registry.read(cls, *args, **kwargs)
File "C:\Python3\lib\site-packages\astropy\io\registry.py", line 517, in read
data = reader(*args, **kwargs)
File "C:\Python3\lib\site-packages\astropy\io\ascii\connect.py", line 20, in read_asciitable
return read(filename, **kwargs)
File "C:\Python3\lib\site-packages\astropy\io\ascii\ui.py", line 390, in read
dat = fast_reader_rdr.read(table)
File "C:\Python3\lib\site-packages\astropy\io\ascii\fastbasic.py", line 128, in read
data, comments = self.engine.read(try_int, try_float, try_string)
File "astropy\io\ascii\cparser.pyx", line 385, in astropy.io.ascii.cparser.CParser.read astropy.io.ascii.core.InconsistentTableError: Number of header columns (22) inconsistent with data columns in data line 0'

我查看了所有地方,更改了 table 上的每个值(浮点数和小数点后两位),检查了新值是否有空白 space 或类似的东西,但是不能'不知道发生了什么。我什至将 Python 版本从 2.7 更改为 3.7,并重新安装了 astropynumpy

如何解决这个问题?

出现此问题是因为您的数据 table 的列数不同,可能是因为缺少数据值。 AstroPy 尝试猜测 header 和 table 的数据部分(您可以在调用 astropy.io.ascii.read() 时用 header_start=data_start= 覆盖,请参阅method definition 在文档中)。 header 用于定义它期望从 table 中读取的列数;如果这在您的数据中途发生变化,您将获得 InconsistentTableError

例如,如果您有一个如下所示的 table(使用散列作为 comment/header 分隔符):

# X Y Z
1 2 3
3 4 5
6   9

那么它会在第 3 行失败,因为现在有 2 列而不是 3 列。您需要确保生成 table 的任何内容都会为 bad/missing 数据 [=26] 填写 'something' =] 这不能被误认为是列之间的分隔符(或在调用中覆盖它)。这可能就是为什么它在转换为 CSV 后 "appeared to work"; CSV 区分了原始 table 数据中可能不存在的常见空白值(空格)和列分隔符(逗号),从而防止了缺失值和列之间的间隙之间的代码混淆。