<p>如果没有更多关于您试图更改的内容的详细信息,很难说,但是假设您使用的是FITS表,最简单的方法可能是使用表API</p>
<p>下面是一个示例表:</p>
<pre class="lang-py prettyprint-override"><code>>>> from astropy.table import Table
>>> t = Table({'a': ['a', 'b', 'c']})
>>> t.write('table.fits')
</code></pre>
<p>您可以使用<code>Table.read</code>加载它,然后像修改任何字符串数组一样修改它。它将正确地处理重新编码</p>
<pre class="lang-py prettyprint-override"><code>>>> t = Table.read('table.fits')
>>> t
<Table length=3>
a
bytes1
a
b
c
>>> t['a'] = ['d', 'e', 'f']
>>> t.write('table.fits', overwrite=True)
>>> t = Table.read('table.fits')
>>> t
<Table length=3>
a
bytes1
d
e
f
</code></pre>
<p>如果您想直接使用较低级别的FITS界面,也可以。在大多数情况下,不必手动构造<code>chararray</code>(这是一个内部实现细节):</p>
<pre class="lang-py prettyprint-override"><code>>>> from astropy.io import fits
>>> hdul = fits.open('table.fits')
>>> hdul[1].data
FITS_rec([('d',), ('e',), ('f',)], dtype=(numpy.record, [('a', 'S1')]))
>>> hdul[1].data['a']
chararray(['d', 'e', 'f'], dtype='<U1')
>>> hdul[1].data['a'] = ['g', 'e', 'h']
>>> hdul[1].data['a']
chararray(['g', 'e', 'h'], dtype='<U1')
>>> hdul.writeto('table.fits', overwrite=True)
>>> hdul = fits.open('table.fits')
>>> hdul[1].data
FITS_rec([('g',), ('e',), ('h',)], dtype=(numpy.record, [('a', 'S1')]))
</code></pre>