Python script utf8 encoding
Talk0
1,599pages on
this wiki
this wiki
When doing python scripts with vim, one can encounter really easily the following error, when giving utf-8 encoded strings from python to vim:
:py print u'echo "\u2026"' Traceback (most recent call last): File "<string>", line 1, in <module> UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' in position 6: ordinal not in range(128)
From a vim mailing list's post, a solution exists that just solves this issue:
python << EOS
uniStr = u"\u2026"
str = uniStr.encode( vim.eval("&encoding") )
print str
EOS
or the following one liner:
:py print u"\u2026".encode( vim.eval("&encoding") )
Don't hesitate reading the whole thread to understand better how vim deals with encoding, and also read :he encoding