Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example asciitable inconsistent_handler routines
"""Provide examples of defining asciitable inconsistent_handler routines to
deal with tables that have rows that are inconsistent with the header
definition.
"""
import asciitable
DEBUG = True
def skip_bad_lines(self, str_vals, ncols):
"""Simply ignore every line with the wrong number of columns."""
if DEBUG:
print 'Skipping line:', ' '.join(str_vals)
return None
def fix_bad_lines(self, str_vals, ncols):
"""Pad with zeros at the end (not enough columns) or truncate
(too many columns)"""
if DEBUG:
print 'Fixing line:', ' '.join(str_vals)
n_str_vals = len(str_vals)
if n_str_vals < ncols:
return str_vals + ['0'] * (ncols - n_str_vals)
else:
return str_vals[:ncols]
# Simple space-delimited ASCII table
table = """
a b c
1 2
4 5 6 7
8 9 10
"""
# When you redefine inconsistent_handler then you must use guess=False and
# provide more information to asciitable about the expected table format.
# Otherwise the inconsistent handler will allow *any* guessed format to parse
# the table, with bad results. In the examples below we use the fact that the
# default Reader is asciitable.BasicReader, which matches the format of the
# example "dat" input.
print 'Plain asciitable (default inconsistent_handler)'
try:
dat = asciitable.read(table, guess=False, delimiter=' ')
except Exception, err:
print 'Got error:', err
print
print 'Using skip_bad_lines()'
asciitable.BaseReader.inconsistent_handler = skip_bad_lines
dat = asciitable.read(table, guess=False, delimiter=' ')
print 'table =', repr(dat)
print
print 'Using fix_bad_lines()'
asciitable.BaseReader.inconsistent_handler = fix_bad_lines
d = asciitable.read(table, guess=False, delimiter=' ')
print 'table =', repr(d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment