Human readable ecsv table
See original GitHub issueI find the ecsv extremely useful, I’m a great fan of including units, comments and descriptions in the tables. The only downside of this format is that I can’t obtain a ecsv (with a comma as a delimiter, since it guarantees max compatibility) that has all the commas aligned. I realize that it is not in the specs of the format to have all the commas aligned, but researchers that I know, that are interested in using tables in astropy, are not adopting it because they can’t read the table with their eyes from the ASCII file. So they try to use fixed-size tables, which produce issue with descriptions and units, leading them to abandon any effort to switch to astropy tables.
I tried to experiment, and a MWE is:
from astropy.table import Table
a = np.array([1.20, 1.21, 2.22])
b = np.array(['u', 'g', 'egr'])
c = np.array([4.20, 1.00, 2.20])
t = Table()
t['a'] = a
t['b'] = b
t['c'] = c
t.write('tmp.txt', format='ascii.ecsv', delimiter=',', strip_whitespace=False, overwrite=True)
resulting in
# ---
# datatype:
# - {name: a, datatype: float64}
# - {name: b, datatype: string}
# - {name: c, datatype: float64}
# delimiter: ','
# schema: astropy-2.0
a,b,c
1.2,u,4.2
1.21,g,1.0
2.22,egr,2.2
I modified the file astropy/astropy/io/ascii/ecsv.py
of the tag v5.0.1, line 383, from return str(col[idx])
to return str(col[idx]).rjust(4)
and as an output I get
# %ECSV 1.0
# ---
# datatype:
# - {name: a, datatype: float64}
# - {name: b, datatype: string}
# - {name: c, datatype: float64}
# delimiter: ','
# schema: astropy-2.0
a,b,c
1.2, u, 4.2
1.21, g, 1.0
2.22, egr, 2.2
which is what I wanted. Of course this is not the way to go, and probably I’m breaking also something somewhere else, but would it be possible to obtain such a feature? My operating system is High Sierra, python 3.9.7 intel, numpy 1.21.2, astropy 5.0.1.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (7 by maintainers)
Top GitHub Comments
One possibility is allowing for column alignment in the space-delimited version of ECSV. In this case it is generally agreed that multiple spaces count as a single delimiter.
See #12821.