开发者

Outputing json with well formed accents

开发者 https://www.devze.com 2022-12-28 18:19 出处:网络
I have an anoying problem that is giving me a hard time these days... I would like to d开发者_StackOverflowevelop a few webservices for my own usage and currently i am fighting with my damn french acc

I have an anoying problem that is giving me a hard time these days... I would like to d开发者_StackOverflowevelop a few webservices for my own usage and currently i am fighting with my damn french accents to be rendered correctly in my json outputs.

Here is my scenario: I retrieve a number of lines from my database that i put in a dict. What i want to do next is pass this dict to json.dumps and output the result.

The problem is: strings containing accents are rendered as utf8 so for example it gives me the following ouput \u00e9milie (it should be émilie). What is frustrating is that if i print each returned line, accents will be correctly rendered in my browser.

Questions:

  • Is it normal to give this kind of json output ?
  • How can I 'simply' transform a dict containing accents to json ? (whis is vital for me since other websites will work with my output)

Here is the test i am running.

# -*- coding: utf-8 -*
from json import dumps as json_dumps
import json

machaine = "une personne émérite"
print(machaine)

output = {}
output[1] = machaine
output[2] = machaine
output[3] = machaine

jsonoutput = json_dumps(output)

print jsonoutput


Just like the docs say, pass ensure_ascii=False and encode manually.

0

精彩评论

暂无评论...
验证码 换一张
取 消