Created
November 20, 2013 18:55
-
-
Save joyrexus/7568844 to your computer and use it in GitHub Desktop.
Pretty-print multi-type lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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