Vim Tips Wiki

Python script utf8 encoding

1,613pages on
this wiki
Revision as of 10:39, January 31, 2014 by JohnBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created March 17, 2013 · complexity basic · version 7.0

When scripting Vim using Python, the following error may occur when passing a utf-8 encoded string 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)

The following example shows how to avoid the problem:

python << EOS
uniStr = u"\u2026"
str = uniStr.encode( vim.eval("&encoding") )
print str

Or, the following one liner:

:py print u"\u2026".encode( vim.eval("&encoding") )

See also


Around Wikia's network

Random Wiki