1

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')

1 Answer 1

2

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

>>> u'\xe4'.encode('utf-8')
'\xc3\xa4'
Sign up to request clarification or add additional context in comments.

1 Comment

Hm ok that works in the with LANG=C python foo.py I could swear I tried that one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.