Fandom

Vim Tips Wiki

Python script utf8 encoding

1,624pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

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
EOS

Or, the following one liner:

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

See alsoEdit

CommentsEdit

Also on Fandom

Random Wiki