How to dump a Python 2.6 dictionary with UTF-8 strings?
Alex Willmer
alex at moreati.org.uk
Tue Jan 11 18:32:31 EST 2011
On Jan 11, 10:40 pm, "W. Martin Borgert" <deba... at debian.org> wrote:
> Hi,
>
> naively, I thought the following code:
>
> #!/usr/bin/env python2.6
> # -*- coding: utf-8 -*-
> import codecs
> d = { u'key': u'我爱中国人' }
> if __name__ == "__main__":
> with codecs.open("ilike.txt", "w", "utf-8") as f:
> print >>f, d
>
> would produce a file ilike.txt like this:
>
> {u'key': u'我爱中国人'}
>
> But unfortunately, it results in:
>
> {u'key': u'\u6211\u7231\u4e2d\u56fd\u4eba'}
>
> What's the right way to get the strings in UTF-8?
>
> Thanks in advance!
It has worked, you're just seeing how python presents unicode
characters in the interactive interpreter:
Python 2.7.1+ (r271:86832, Dec 24 2010, 10:04:43)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = {u'key': u'\u6211\u7231\u4e2d\u56fd\u4eba'}
>>> x
{u'key': u'\u6211\u7231\u4e2d\u56fd\u4eba'}
>>> print x
{u'key': u'\u6211\u7231\u4e2d\u56fd\u4eba'}
>>> print x['key']
我爱中国人
That last line only works if your terminal uses an suitable encoding
(e.g. utf-8).
Regards, Alex
More information about the Python-list
mailing list