Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Created November 20, 2013 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joyrexus/7568844 to your computer and use it in GitHub Desktop.
Save joyrexus/7568844 to your computer and use it in GitHub Desktop.
Pretty-print multi-type lists
# Pretty-print lists containing ints, strings, and unicode
delim = "\t" # use tab as delimiter
def convert(x):
'Coerce ints to strings, otherwise try to encode.'
return str(x) if type(x) is int else x.encode('utf-8', 'ignore')
def pprint(args):
'Pretty-print list of args.'
try:
print delim.join(str(i) for i in args)
except UnicodeEncodeError:
print delim.join(convert(i) for i in args)
mixed = [1, 'A', u'\xe9']
pprint(mixed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment