def _ascii_generate_header(tab, comments='#', delimiter=' ',
commentedHeader=True):
""" Generate the corresponding ascii Header that contains all necessary info
Parameters
----------
tab: SimpleTable instance
table
comments: str
string to prepend header lines
delimiter: str, optional
The string used to separate values. By default, this is any
whitespace.
commentedHeader: bool, optional
if set, the last line of the header is expected to be the column titles
Returns
-------
hdr: str
string that will be be written at the beginning of the file
"""
hdr = []
if comments is None:
comments = ''
# table header
length = max(map(len, tab.header.keys()))
fmt = '{{0:s}} {{1:{0:d}s}}\t{{2:s}}'.format(length)
for k, v in tab.header.items():
for vk in v.split('\n'):
if len(vk) > 0:
hdr.append(fmt.format(comments, k.upper(), vk.strip()))
# column metadata
hdr.append(comments) # add empty line
length = max(map(len, tab.keys()))
fmt = '{{0:s}}{{0:s}} {{1:{0:d}s}}\t{{2:s}}\t{{3:s}}'.format(length)
for colname in tab.keys():
unit = tab._units.get(colname, 'None')
desc = tab._desc.get(colname, 'None')
hdr.append(fmt.format(comments, colname, unit, desc))
# aliases
if len(tab._aliases) > 0:
hdr.append(comments) # add empty line
for k, v in tab._aliases.items():
hdr.append('{0:s} alias\t{1:s}={2:s}'.format(comments, k, v))
# column names
hdr.append(comments)
if commentedHeader:
hdr.append('{0:s} {1:s}'.format(comments, delimiter.join(tab.keys())))
else:
hdr.append('{0:s}'.format(delimiter.join(tab.keys())))
return '\n'.join(hdr)
评论列表
文章目录