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 :~)
--
h
On Jan 11, 10:40 pm, "W. Martin Borgert" 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 >>
> 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, ma
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'我爱中国人'}