pythoncsvencodingastropyfits

Python Astropy: encoding error while converting a .csv file to .fits


I have a .csv file (comma separated data) where one column contains special symbols like α (alpha), while other columns contains strings, int or floats.

I try to read this csv file and write it into a .fits file using this code:

fits.writeto(target_file.fits, np.array(Table.read(source_file.csv)))

But at the α (alpha) symbol position, the code throws me an error:

'ascii' codec can't encode characters in position 2-3: ordinal not in range(128)

I suppose I have to encode something in utf-8, and I tried different solutions without success. May you please help me ?


Solution

  • FITS only supports ASCII. You should be able to do something like below where you manually encode to bytes and then decode back to unicode:

    >>> from astropy.table.table_helpers import simple_table
    >>> from astropy.table import Table
    >>> t = simple_table()
    >>> t['c'] = "α"
    >>> t
    <Table length=3>
      a      b     c  
    int64 float64 str1
    ----- ------- ----
        1     1.0    α
        2     2.0    α
        3     3.0    α
    >>> t.convert_unicode_to_bytestring()
    >>> t
    <Table length=3>
      a      b      c   
    int64 float64 bytes2
    ----- ------- ------
        1     1.0      α
        2     2.0      α
        3     3.0      α
    >>> t.write('simple_encoded.fits')
    >>> t2 = Table.read('simple_encoded.fits')
    >>> t2.convert_bytestring_to_unicode()
    >>> t2
    <Table length=3>
      a      b     c  
    int64 float64 str1
    ----- ------- ----
        1     1.0    α
        2     2.0    α
        3     3.0    α