Dealing with unicode in Python

Posted on by 2 comments

I say transliterating, but I really just mean coping with strings involving unprintable characters…

 

Today I came across a neat little trick which I hope helps you as much as I.

take the following print statement

print "blahÆlec"

this will break

but there are some cases where exact spelling isn’t important you just to to out put the data so you can glace at it, you can actually give the str decode method a nifty argument “ignore” eg.

print "blahÆlec".decode("ascii","ignore")

and this simply drops the unprintable characters.

Success

Category: Python | Tags: , ,

2 comments on “Dealing with unicode in Python

  1. Or, if you have a half-decent terminal emulator, you can just encode it to utf-8, and get the correct character printed instead of mangling the string.

    • This was actually for sanitizing the results of a db2 query to be written to a xlsx spreadsheet and the unicode seemed to kill it

Leave a Reply

Your email address will not be published. Required fields are marked *