How to dump a Python 2.6 dictionary with UTF-8 strings?

2011-01-11 Thread W. Martin Borgert
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

Re: How to dump a Python 2.6 dictionary with UTF-8 strings?

2011-01-11 Thread Martin v. Loewis
What's the right way to get the strings in UTF-8? This will work. I doubt you can get it much simpler in 2.x; in 3.x, your code will work out of the box (with proper syntactical adjustments). import pprint, cStringIO class UniPrinter(pprint.PrettyPrinter): def format(self, obj, context,

Re: How to dump a Python 2.6 dictionary with UTF-8 strings?

2011-01-11 Thread Alex Willmer
On Jan 11, 10:40 pm, W. Martin Borgert deba...@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

Re: How to dump a Python 2.6 dictionary with UTF-8 strings?

2011-01-11 Thread W. Martin Borgert
On 2011-01-12 00:27, Martin v. Loewis wrote: This will work. I doubt you can get it much simpler in 2.x; in 3.x, your code will work out of the box (with proper syntactical adjustments). Thanks, this works like a charm. I tried pprint before for this task and failed. Now I know why :~) --