[IPython-dev] can't %edit python statement inclued non-ascii charset

Michael McLay michael.j.mclay at gmail.com
Wed Mar 12 08:22:06 EDT 2008


The error is triggered when the edit command writes the string to a
temporary file using the builtin open function. The codecs.open
function will write a non-ascii character to a file as the following
shows.

Python 2.5.1 (r251:54863, May  2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = u'\245\200'
>>> open('test.uraw', 'w').write(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position
0-1: ordinal not in range(128)
>>> import codecs
>>> codecs.open('test.uraw', 'w',encoding='utf-8').write(s)
>>>


Andrew Kuchling wrote a very informative tutorial on using Unicode in Python.

    http://www.amk.ca/python/howto/unicode



2008/3/12 tocer <tocer.deng at gmail.com>:
> hi:
>
>  I typed a python statement inclued non_ascii charset,such as:
>
>  In [1] : s = '你好'   # it's a Chinses string
>
>  when I wanted to edit the statement again by typing edit command:
>
>  In [2]: edit In[1]
>
>  a error occured:
>
>  UnicodeEncodeError: 'ascii' codec can't encode characters in
>  position 5-6: ordinal not in range(128)
>
>  how could I make the mistake disapper.
>
>  btw:my box is ubuntu7.04, python2.5, ipython8.2
>
>  thanks.
>
>  --tocer
>
>
>  _______________________________________________
>  IPython-dev mailing list
>  IPython-dev at scipy.org
>  http://lists.ipython.scipy.org/mailman/listinfo/ipython-dev
>


More information about the IPython-dev mailing list