我有一个 3d 天文表,我想将其写入文件。我通过将 3d numpy 数组转换为 astropy 表并添加一些列名来创建它。
这是我的表的示例输出。每行每列中的数组表示第三维。
m_star [14] m_bh [14] ... exit_flag [14]
float64 float64 ... float64
------------------------- ------------------------ ... --------------
3.9782e+32 .. 2.18801e+33 1.9891e+36 .. 1.9891e+38 ... 0.0 .. 0.0
3.9782e+32 .. 2.18801e+33 1.9891e+36 .. 1.9891e+38 ... 0.0 .. 0.0
3.9782e+32 .. 2.18801e+33 1.9891e+36 .. 1.9891e+38 ... 0.0 .. 0.0
3.9782e+32 .. 2.18801e+33 1.9891e+36 .. 1.9891e+38 ... 0.0 .. 0.0
3.9782e+32 .. 2.18801e+33 1.9891e+36 .. 1.9891e+38 ... 0.0 .. 0.0
3.9782e+32 .. 2.18801e+33 1.9891e+36 .. 1.9891e+38 ... 6.0 .. 6.0
我将 2d astropy 表写入文件的方式是
param_table.write(param.txt, format='ascii')
但是当我尝试将 3d astropy 表写入文件时,我收到错误:
Traceback (most recent call last):
File "/Users/astromonerd/HLX1/src/make_dat.py", line 86, in <module>
dat_table.write(run_dir + '/' + 'dat.txt', format='ascii')
File "/usr/local/lib/python2.7/site-packages/astropy/table/table.py", line 1906, in write
io_registry.write(self, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/astropy/io/registry.py", line 374, in write
writer(data, *args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/astropy/io/ascii/connect.py", line 29, in write_asciitable
return write(table, filename, **kwargs)
File "/usr/local/lib/python2.7/site-packages/astropy/io/ascii/ui.py", line 458, in write
writer.write(table, output)
File "/usr/local/lib/python2.7/site-packages/astropy/io/ascii/fastbasic.py", line 125, in write
self._write(table, output, {})
File "/usr/local/lib/python2.7/site-packages/astropy/io/ascii/fastbasic.py", line 139, in _write
writer.write(output, header_output, output_types)
File "astropy/io/ascii/cparser.pyx", line 986, in astropy.io.ascii.cparser.FastWriter.write (astropy/io/ascii/cparser.c:17696)
TypeError: unhashable type: 'list'
关键是能够通过另一个模块读取文件并检索 astropy 表。