buergi
buergi

Reputation: 6256

How to print Unicode in Python 2 when LANG=C

Quite a silly question, I know. Of course normally LANG=C indicates an ASCII terminal which cannot display Unicode characters. But I nevertheless want to print out the UTF-8 bytes. I use Python 2 (2.6.5 actually)

print '\xc3\xa4', u'\xe4'

This prints 'ä ä' on a Unicode terminal, but the second string causes an error when executed with LANG=C. I don't want Python to be smart but simply convert u'\xe4' to UTF-8 so it's just '\xc3\xa4' in memory.

I tried all combinations of decode(), encode() and unicode() that I can imagine but it seems I missed the right combination.

What I actually want is reading Unicode charaters through vi's system() function, like

:echo system('python foo.py')

Upvotes: 1

Views: 546

Answers (1)

Wooble
Wooble

Reputation: 90007

To encode a unicode to utf-8, call .encode('utf-8') on it.:

>>> u'\xe4'.encode('utf-8')
'\xc3\xa4'

Upvotes: 2

Related Questions