Inserting a UTF-8 character using Python

I was processing a UTF-8 dataset where the performance copyright symbol (Unicode code point 0x2117) had been entered as ASCII “(P)”. I wanted to replace this sequence with the actual symbol, but I didn’t know how to create an arbitrary UTF-8 character. After a bit of googling (more than 5 mins, otherwise I wouldn’t be posting!) here’s the answer:

txt = re.sub(r'[(]P[)]', unichr(0x2117).encode('utf-8'), txt)
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

%d bloggers like this: