Wikia

Vim Tips Wiki

Changes: Python script utf8 encoding

Edit

Back to page

(Created page with "When doing python scripts with vim, one can encounter really easily the following error, when giving utf-8 encoded strings from python to vim: <pre> :py print u'echo "\u2026"...")
 
(Insert TipProposed template + manual clean)
 
Line 1: Line 1:
When doing python scripts with vim, one can encounter really easily the following error, when giving utf-8 encoded strings from python to vim:
+
{{TipProposed
+
|id=0
  +
|previous=0
  +
|next=0
  +
|created=March 17, 2013
  +
|complexity=basic
  +
|author=
  +
|version=7.0
  +
|subpage=/2013
  +
|category1=Python
  +
|category2=Scripting
  +
}}
  +
When scripting Vim using Python, the following error may occur when passing a utf-8 encoded string from Python to Vim:
 
<pre>
 
<pre>
 
:py print u'echo "\u2026"'
 
:py print u'echo "\u2026"'
Line 6: Line 6:
 
File "<string>", line 1, in <module>
 
File "<string>", line 1, in <module>
 
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' in
 
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2026' in
position 6: ordinal not in range(128)
+
position 6: ordinal not in range(128)
 
</pre>
 
</pre>
   
From a [https://groups.google.com/forum/?fromgroups=#!searchin/vim_use/python$20utf-8/vim_use/XXVGOuPkszQ/K6LmCqM2GKAJ vim mailing list's post], a solution exists that just solves this issue:
+
The following example shows how to avoid the problem:
 
 
<pre>
 
<pre>
 
python << EOS
 
python << EOS
Line 15: Line 15:
 
str = uniStr.encode( vim.eval("&encoding") )
 
str = uniStr.encode( vim.eval("&encoding") )
 
print str
 
print str
EOS
+
EOS
 
</pre>
 
</pre>
   
or the following one liner:
+
Or, the following one liner:
 
 
<pre>
 
<pre>
 
:py print u"\u2026".encode( vim.eval("&encoding") )
 
:py print u"\u2026".encode( vim.eval("&encoding") )
 
</pre>
 
</pre>
   
Don't hesitate reading the whole thread to understand better how vim deals with encoding, and also read :he encoding
+
==See also==
[[Category:Python]]
+
*[https://groups.google.com/forum/?fromgroups=#!searchin/vim_use/python$20utf-8/vim_use/XXVGOuPkszQ/K6LmCqM2GKAJ Vim mailing list] origin of this tip: see discussion for more information
[[Category:Scripting]]
+
[[Category:Utf8]]
+
==Comments==

Latest revision as of 10:39, January 31, 2014

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

Around Wikia's network

Random Wiki