The hacker’s toolbox

I spend a lot of time hacking around in Python, and lately in R. Here are some things I’ve learned, or relearned repeatedly.

Python and Unicode

My single biggest frustration in working with Python is the arcane support for Unicode. If you’re hacking in the shell and Unicode is giving you fits, this may help:

# reading a unicode string
text = open('foo.html').read().decode('utf8')

# printing that string
print text.encode('utf8')

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s