[IronPython] how to change default encoding of string and file at runtime?

Seo Sanghyeon tinuviel at sparcs.kaist.ac.kr
Wed Dec 28 04:01:12 CET 2005


On Tue, Dec 27, 2005 at 04:57:32PM -0800, Dino Viehland wrote:
> This is an interesting issue...  If I take your same code and run it
> on CPython it raises an exception: (snip)

Actually, this works when you set "defaultencoding".

$ python
Python 2.3.5 (#2, Nov 20 2005, 16:40:39)
[GCC 4.0.3 20051111 (prerelease) (Debian 4.0.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = u'\u6211'
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>> str(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode character u'\u6211' in position 0: ordinal not in range(128)
>>> reload(sys)
<module 'sys' (built-in)>
>>> sys.setdefaultencoding('gbk')
>>> sys.getdefaultencoding()
'gbk'
>>> str(s)
'\xce\xd2'
>>>

Hope this help,
Seo Sanghyeon



More information about the Ironpython-users mailing list