用JSON.DUMPS作为UTF-8保存UTF-8文本,而不是\ U逃脱序列

发布于 2025-01-27 06:12:24 字数 407 浏览 7 评论 0 原文

import json
json_string = json.dumps("ברי צקלה")
print(json_string)

在A repl

"\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4"

示例代码( 。我的(智能)用户想使用JSON转储验证甚至编辑文本文件(我宁愿不使用XML)。

有没有办法将对象序列化为UTF-8 JSON字符串(而不是 \ uxxxx )?

Sample code (in a REPL):

import json
json_string = json.dumps("ברי צקלה")
print(json_string)

Output:

"\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4"

The problem: it's not human readable. My (smart) users want to verify or even edit text files with JSON dumps (and I’d rather not use XML).

Is there a way to serialize objects into UTF-8 JSON strings (instead of \uXXXX)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

财迷小姐 2025-02-03 06:12:24

使用 huse_ascii = false 切换到 json.dumps(),然后手动将值编码为utf-8:

>>> json_string = json.dumps("ברי צקלה", ensure_ascii=False).encode('utf8')
>>> json_string
b'"\xd7\x91\xd7\xa8\xd7\x99 \xd7\xa6\xd7\xa7\xd7\x9c\xd7\x94"'
>>> print(json_string.decode())
"ברי צקלה"

如果您写入文件,只需使用> json.dump(),然后将其放在文件对象中以进行编码:

with open('filename', 'w', encoding='utf8') as json_file:
    json.dump("ברי צקלה", json_file, ensure_ascii=False)

python 2的python 2

python 2的警告,还有更多的警告需要考虑。如果将其写入文件,则可以使用 io。 open() 而不是 open()来生成一个文件对象,该文件对您编写Unicode值,然后使用 json.dump()代替写入该文件:

with io.open('filename', 'w', encoding='utf8') as json_file:
    json.dump(u"ברי צקלה", json_file, ensure_ascii=False)

请注意,有一个 json 模块中的错误 huse_ascii = false flag可以产生 unicode str 对象的 mix 。然后,Python 2的解决方法是:

with io.open('filename', 'w', encoding='utf8') as json_file:
    data = json.dumps(u"ברי צקלה", ensure_ascii=False)
    # unicode(data) auto-decodes data to unicode if str
    json_file.write(unicode(data))

在Python 2中,使用字节字符串(类型 str ),编码为UTF-8时,请确保还设置编码>编码键字:

>>> d={ 1: "ברי צקלה", 2: u"ברי צקלה" }
>>> d
{1: '\xd7\x91\xd7\xa8\xd7\x99 \xd7\xa6\xd7\xa7\xd7\x9c\xd7\x94', 2: u'\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4'}

>>> s=json.dumps(d, ensure_ascii=False, encoding='utf8')
>>> s
u'{"1": "\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4", "2": "\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4"}'
>>> json.loads(s)['1']
u'\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4'
>>> json.loads(s)['2']
u'\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4'
>>> print json.loads(s)['1']
ברי צקלה
>>> print json.loads(s)['2']
ברי צקלה

Use the ensure_ascii=False switch to json.dumps(), then encode the value to UTF-8 manually:

>>> json_string = json.dumps("ברי צקלה", ensure_ascii=False).encode('utf8')
>>> json_string
b'"\xd7\x91\xd7\xa8\xd7\x99 \xd7\xa6\xd7\xa7\xd7\x9c\xd7\x94"'
>>> print(json_string.decode())
"ברי צקלה"

If you are writing to a file, just use json.dump() and leave it to the file object to encode:

with open('filename', 'w', encoding='utf8') as json_file:
    json.dump("ברי צקלה", json_file, ensure_ascii=False)

Caveats for Python 2

For Python 2, there are some more caveats to take into account. If you are writing this to a file, you can use io.open() instead of open() to produce a file object that encodes Unicode values for you as you write, then use json.dump() instead to write to that file:

with io.open('filename', 'w', encoding='utf8') as json_file:
    json.dump(u"ברי צקלה", json_file, ensure_ascii=False)

Do note that there is a bug in the json module where the ensure_ascii=False flag can produce a mix of unicode and str objects. The workaround for Python 2 then is:

with io.open('filename', 'w', encoding='utf8') as json_file:
    data = json.dumps(u"ברי צקלה", ensure_ascii=False)
    # unicode(data) auto-decodes data to unicode if str
    json_file.write(unicode(data))

In Python 2, when using byte strings (type str), encoded to UTF-8, make sure to also set the encoding keyword:

>>> d={ 1: "ברי צקלה", 2: u"ברי צקלה" }
>>> d
{1: '\xd7\x91\xd7\xa8\xd7\x99 \xd7\xa6\xd7\xa7\xd7\x9c\xd7\x94', 2: u'\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4'}

>>> s=json.dumps(d, ensure_ascii=False, encoding='utf8')
>>> s
u'{"1": "\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4", "2": "\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4"}'
>>> json.loads(s)['1']
u'\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4'
>>> json.loads(s)['2']
u'\u05d1\u05e8\u05d9 \u05e6\u05e7\u05dc\u05d4'
>>> print json.loads(s)['1']
ברי צקלה
>>> print json.loads(s)['2']
ברי צקלה
ゝ偶尔ゞ 2025-02-03 06:12:24

写入文件

import codecs
import json

with codecs.open('your_file.txt', 'w', encoding='utf-8') as f:
    json.dump({"message":"xin chào việt nam"}, f, ensure_ascii=False)

以打印到stdout

import json
print(json.dumps({"message":"xin chào việt nam"}, ensure_ascii=False))

To write to a file

import codecs
import json

with codecs.open('your_file.txt', 'w', encoding='utf-8') as f:
    json.dump({"message":"xin chào việt nam"}, f, ensure_ascii=False)

To print to stdout

import json
print(json.dumps({"message":"xin chào việt nam"}, ensure_ascii=False))
你不是我要的菜∠ 2025-02-03 06:12:24

感谢您在这里的原始答案。使用Python  3以下代码行:

print(json.dumps(result_dict,ensure_ascii=False))

还可以。如果不是必须在代码中写太多文本,请考虑不要在代码中写太多文本。

对于Python控制台来说,这可能足够好。但是,要满足服务器,您可能需要设置此处所述(如果它在Apache上)

基本上,安装he_il或ubuntu上的任何语言语言环境。
检查它未安装:

locale -a

安装它,其中xx是您的语言:

sudo apt-get install language-pack-XX

例如:

sudo apt-get install language-pack-he

将以下文本添加到/etc/apache2/envvrs

export LANG='he_IL.UTF-8'
export LC_ALL='he_IL.UTF-8'

中,您希望您不会从Apache上遇到Python错误:

打印(JS)
UnicodeCodeError:“ ASCII”编解码器无法在位置41-45中编码字符:不在范围内(128)

Apache中的范围内(128),尝试使UTF默认编码如下所述:
在utf-8对于Apache

做到这一点,因为Apache错误可能会引起调试很痛苦,并且您会错误地认为这是来自Python的,这在这种情况下可能并非如此。

Thanks for the original answer here. With Python 3 the following line of code:

print(json.dumps(result_dict,ensure_ascii=False))

was ok. Consider trying not writing too much text in the code if it's not imperative.

This might be good enough for the Python console. However, to satisfy a server, you might need to set the locale as explained here (if it is on Apache 2)
Setting LANG and LC_ALL when using mod_wsgi

Basically, install he_IL or whatever language locale on Ubuntu.
Check it is not installed:

locale -a

Install it, where XX is your language:

sudo apt-get install language-pack-XX

For example:

sudo apt-get install language-pack-he

Add the following text to /etc/apache2/envvrs

export LANG='he_IL.UTF-8'
export LC_ALL='he_IL.UTF-8'

Then you would hopefully not get Python errors on from Apache like:

print (js)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 41-45: ordinal not in range(128)

Also in Apache, try to make UTF the default encoding as explained here:
How to change the default encoding to UTF-8 for Apache

Do it early because Apache errors can be pain to debug and you can mistakenly think it's from Python which possibly isn't the case in that situation.

给我一枪 2025-02-03 06:12:24

从Python 3.7开始,以下代码正常工作:

from json import dumps
result = {"symbol": "ƒ"}
json_string = dumps(result, sort_keys=True, indent=2, ensure_ascii=False)
print(json_string)

输出:

{"symbol": "ƒ"}

As of Python 3.7 the following code works fine:

from json import dumps
result = {"symbol": "ƒ"}
json_string = dumps(result, sort_keys=True, indent=2, ensure_ascii=False)
print(json_string)

Output:

{"symbol": "ƒ"}
梦开始←不甜 2025-02-03 06:12:24

这是错误的答案,但是了解为什么错误仍然很有用。请参阅评论。

使用 Unicode-Escape

>>> d = {1: "ברי צקלה", 2: u"ברי צקלה"}
>>> json_str = json.dumps(d).decode('unicode-escape').encode('utf8')
>>> print json_str
{"1": "ברי צקלה", "2": "ברי צקלה"}

This is the wrong answer, but it's still useful to understand why it's wrong. See comments.

Use unicode-escape:

>>> d = {1: "ברי צקלה", 2: u"ברי צקלה"}
>>> json_str = json.dumps(d).decode('unicode-escape').encode('utf8')
>>> print json_str
{"1": "ברי צקלה", "2": "ברי צקלה"}
方觉久 2025-02-03 06:12:24

Pieters的Python 2解决方法在边缘案例上失败:

d = {u'keyword': u'bad credit  \xe7redit cards'}
with io.open('filename', 'w', encoding='utf8') as json_file:
    data = json.dumps(d, ensure_ascii=False).decode('utf8')
    try:
        json_file.write(data)
    except TypeError:
        # Decode data to Unicode first
        json_file.write(data.decode('utf8'))

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in position 25: ordinal not in range(128)

它在第3行的.decode('utf8')的一部分上崩溃。我通过避免使用该步骤来使程序更简单地解决了该问题,从而解决了问题作为ASCII的特殊套管:

with io.open('filename', 'w', encoding='utf8') as json_file:
  data = json.dumps(d, ensure_ascii=False, encoding='utf8')
  json_file.write(unicode(data))

cat filename
{"keyword": "bad credit  çredit cards"}

Pieters' Python 2 workaround fails on an edge case:

d = {u'keyword': u'bad credit  \xe7redit cards'}
with io.open('filename', 'w', encoding='utf8') as json_file:
    data = json.dumps(d, ensure_ascii=False).decode('utf8')
    try:
        json_file.write(data)
    except TypeError:
        # Decode data to Unicode first
        json_file.write(data.decode('utf8'))

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in position 25: ordinal not in range(128)

It was crashing on the .decode('utf8') part of line 3. I fixed the problem by making the program much simpler by avoiding that step as well as the special casing of ASCII:

with io.open('filename', 'w', encoding='utf8') as json_file:
  data = json.dumps(d, ensure_ascii=False, encoding='utf8')
  json_file.write(unicode(data))

cat filename
{"keyword": "bad credit  çredit cards"}
风吹雨成花 2025-02-03 06:12:24

使用 unicode-escape 来解决问题

>>>import json
>>>json_string = json.dumps("ברי צקלה")
>>>json_string.encode('ascii').decode('unicode-escape')
'"ברי צקלה"'

说明

>>>s = '漢  χαν  хан'
>>>print('Unicode: ' + s.encode('unicode-escape').decode('utf-8'))

Unicode: \u6f22  \u03c7\u03b1\u03bd  \u0445\u0430\u043d

>>>u = s.encode('unicode-escape').decode('utf-8')
>>>print('Original: ' + u.encode("utf-8").decode('unicode-escape'))

Original: 漢  χαν  хан

原始资源: python3使用unicode-escape处理unicode 16

Use unicode-escape to solve the problem

>>>import json
>>>json_string = json.dumps("ברי צקלה")
>>>json_string.encode('ascii').decode('unicode-escape')
'"ברי צקלה"'

Explanation

>>>s = '漢  χαν  хан'
>>>print('Unicode: ' + s.encode('unicode-escape').decode('utf-8'))

Unicode: \u6f22  \u03c7\u03b1\u03bd  \u0445\u0430\u043d

>>>u = s.encode('unicode-escape').decode('utf-8')
>>>print('Original: ' + u.encode("utf-8").decode('unicode-escape'))

Original: 漢  χαν  хан

Original resource:Python3 使用 unicode-escape 处理 unicode 16进制字符串编解码问题

予囚 2025-02-03 06:12:24

如果要从文件加载JSON字符串,而文件内容是阿拉伯语文本,则可以正常工作。

假设像 arabic.json 这样的文件

{
  "key1": "لمستخدمين",
  "key2": "إضافة مستخدم"
}

arabic.json.json 文件中获取阿拉伯内容,

with open(arabic.json, encoding='utf-8') as f:
   # Deserialises it
   json_data = json.load(f)
   f.close()

# JSON formatted string
json_data2 = json.dumps(json_data, ensure_ascii = False)

以在Django模板中使用JSON数据,请按照以下步骤:

# If have to get the JSON index in a Django template file, then simply decode the encoded string.

json.JSONDecoder().decode(json_data2)

完成!现在,我们可以将结果作为具有阿拉伯值的JSON指数。

If you are loading a JSON string from a file and the file content is Arabic texts, then this will work.

Assume a file like arabic.json

{
  "key1": "لمستخدمين",
  "key2": "إضافة مستخدم"
}

Get the Arabic contents from the arabic.json file

with open(arabic.json, encoding='utf-8') as f:
   # Deserialises it
   json_data = json.load(f)
   f.close()

# JSON formatted string
json_data2 = json.dumps(json_data, ensure_ascii = False)

To use JSON data in a Django template follow the below steps:

# If have to get the JSON index in a Django template file, then simply decode the encoded string.

json.JSONDecoder().decode(json_data2)

Done! Now we can get the results as a JSON index with Arabic values.

故笙诉离歌 2025-02-03 06:12:24

以下是我的理解量阅读答案和Google。

# coding:utf-8
r"""
@update: 2017-01-09 14:44:39
@explain: str, unicode, bytes in python2to3
    #python2 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 7: ordinal not in range(128)
    #1.reload
    #importlib,sys
    #importlib.reload(sys)
    #sys.setdefaultencoding('utf-8') #python3 don't have this attribute.
    #not suggest even in python2 #see:http://stackoverflow.com/questions/3828723/why-should-we-not-use-sys-setdefaultencodingutf-8-in-a-py-script
    #2.overwrite /usr/lib/python2.7/sitecustomize.py or (sitecustomize.py and PYTHONPATH=".:$PYTHONPATH" python)
    #too complex
    #3.control by your own (best)
    #==> all string must be unicode like python3 (u'xx'|b'xx'.encode('utf-8')) (unicode 's disappeared in python3)
    #see: http://blog.ernest.me/post/python-setdefaultencoding-unicode-bytes

    #how to Saving utf-8 texts in json.dumps as UTF8, not as \u escape sequence
    #http://stackoverflow.com/questions/18337407/saving-utf-8-texts-in-json-dumps-as-utf8-not-as-u-escape-sequence
"""

from __future__ import print_function
import json

a = {"b": u"中文"}  # add u for python2 compatibility
print('%r' % a)
print('%r' % json.dumps(a))
print('%r' % (json.dumps(a).encode('utf8')))
a = {"b": u"中文"}
print('%r' % json.dumps(a, ensure_ascii=False))
print('%r' % (json.dumps(a, ensure_ascii=False).encode('utf8')))
# print(a.encode('utf8')) #AttributeError: 'dict' object has no attribute 'encode'
print('')

# python2:bytes=str; python3:bytes
b = a['b'].encode('utf-8')
print('%r' % b)
print('%r' % b.decode("utf-8"))
print('')

# python2:unicode; python3:str=unicode
c = b.decode('utf-8')
print('%r' % c)
print('%r' % c.encode('utf-8'))
"""
#python2
{'b': u'\u4e2d\u6587'}
'{"b": "\\u4e2d\\u6587"}'
'{"b": "\\u4e2d\\u6587"}'
u'{"b": "\u4e2d\u6587"}'
'{"b": "\xe4\xb8\xad\xe6\x96\x87"}'

'\xe4\xb8\xad\xe6\x96\x87'
u'\u4e2d\u6587'

u'\u4e2d\u6587'
'\xe4\xb8\xad\xe6\x96\x87'

#python3
{'b': '中文'}
'{"b": "\\u4e2d\\u6587"}'
b'{"b": "\\u4e2d\\u6587"}'
'{"b": "中文"}'
b'{"b": "\xe4\xb8\xad\xe6\x96\x87"}'

b'\xe4\xb8\xad\xe6\x96\x87'
'中文'

'中文'
b'\xe4\xb8\xad\xe6\x96\x87'
"""

The following is my understanding var reading answer above and google.

# coding:utf-8
r"""
@update: 2017-01-09 14:44:39
@explain: str, unicode, bytes in python2to3
    #python2 UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 7: ordinal not in range(128)
    #1.reload
    #importlib,sys
    #importlib.reload(sys)
    #sys.setdefaultencoding('utf-8') #python3 don't have this attribute.
    #not suggest even in python2 #see:http://stackoverflow.com/questions/3828723/why-should-we-not-use-sys-setdefaultencodingutf-8-in-a-py-script
    #2.overwrite /usr/lib/python2.7/sitecustomize.py or (sitecustomize.py and PYTHONPATH=".:$PYTHONPATH" python)
    #too complex
    #3.control by your own (best)
    #==> all string must be unicode like python3 (u'xx'|b'xx'.encode('utf-8')) (unicode 's disappeared in python3)
    #see: http://blog.ernest.me/post/python-setdefaultencoding-unicode-bytes

    #how to Saving utf-8 texts in json.dumps as UTF8, not as \u escape sequence
    #http://stackoverflow.com/questions/18337407/saving-utf-8-texts-in-json-dumps-as-utf8-not-as-u-escape-sequence
"""

from __future__ import print_function
import json

a = {"b": u"中文"}  # add u for python2 compatibility
print('%r' % a)
print('%r' % json.dumps(a))
print('%r' % (json.dumps(a).encode('utf8')))
a = {"b": u"中文"}
print('%r' % json.dumps(a, ensure_ascii=False))
print('%r' % (json.dumps(a, ensure_ascii=False).encode('utf8')))
# print(a.encode('utf8')) #AttributeError: 'dict' object has no attribute 'encode'
print('')

# python2:bytes=str; python3:bytes
b = a['b'].encode('utf-8')
print('%r' % b)
print('%r' % b.decode("utf-8"))
print('')

# python2:unicode; python3:str=unicode
c = b.decode('utf-8')
print('%r' % c)
print('%r' % c.encode('utf-8'))
"""
#python2
{'b': u'\u4e2d\u6587'}
'{"b": "\\u4e2d\\u6587"}'
'{"b": "\\u4e2d\\u6587"}'
u'{"b": "\u4e2d\u6587"}'
'{"b": "\xe4\xb8\xad\xe6\x96\x87"}'

'\xe4\xb8\xad\xe6\x96\x87'
u'\u4e2d\u6587'

u'\u4e2d\u6587'
'\xe4\xb8\xad\xe6\x96\x87'

#python3
{'b': '中文'}
'{"b": "\\u4e2d\\u6587"}'
b'{"b": "\\u4e2d\\u6587"}'
'{"b": "中文"}'
b'{"b": "\xe4\xb8\xad\xe6\x96\x87"}'

b'\xe4\xb8\xad\xe6\x96\x87'
'中文'

'中文'
b'\xe4\xb8\xad\xe6\x96\x87'
"""
银河中√捞星星 2025-02-03 06:12:24

这是使用json.dump()的我的解决方案:

def jsonWrite(p, pyobj, ensure_ascii=False, encoding=SYSTEM_ENCODING, **kwargs):
    with codecs.open(p, 'wb', 'utf_8') as fileobj:
        json.dump(pyobj, fileobj, ensure_ascii=ensure_ascii,encoding=encoding, **kwargs)

将System_Encoding设置为:

locale.setlocale(locale.LC_ALL, '')
SYSTEM_ENCODING = locale.getlocale()[1]

Here's my solution using json.dump():

def jsonWrite(p, pyobj, ensure_ascii=False, encoding=SYSTEM_ENCODING, **kwargs):
    with codecs.open(p, 'wb', 'utf_8') as fileobj:
        json.dump(pyobj, fileobj, ensure_ascii=ensure_ascii,encoding=encoding, **kwargs)

where SYSTEM_ENCODING is set to:

locale.setlocale(locale.LC_ALL, '')
SYSTEM_ENCODING = locale.getlocale()[1]
人疚 2025-02-03 06:12:24

如果可能的话,请使用编解码器

with codecs.open('file_path', 'a+', 'utf-8') as fp:
    fp.write(json.dumps(res, ensure_ascii=False))

Use codecs if possible,

with codecs.open('file_path', 'a+', 'utf-8') as fp:
    fp.write(json.dumps(res, ensure_ascii=False))
养猫人 2025-02-03 06:12:24

这不是完全答案,但无论如何它可能会有所帮助...
我经常使用以下命令将“转换” json-with- \ uxxxx-chars到json-with-with-utf8:

jq . json-with-uxxxx-chars.json > json-with-utf8.json

jq 是处理JSON的绝妙工具。在这里,我们仅使用识别功能
(像往常一样

(this is not exactly an answer, but it may help anyway...)
I often use the following command to "convert" json-with-\uxxxx-chars to json-with-utf8:

jq . json-with-uxxxx-chars.json > json-with-utf8.json

jq is a fantastic tool to process json. Here we are using just identify-function.
(As usual, apt install jq, if not yet installed)

画中仙 2025-02-03 06:12:24

json.dumps 中使用 suse_ascii = false 是解决此问题的正确方向,如 Martijn 指出。但是,这可能会导致一个例外:

unicodedecodeerror:'ascii'编解码器无法在位置1中解码字节0xe7:ordinal不在范围内(128)

您需要额外的设置。设置您的 sys.getDefaultEncoding()正确。 site.py lib/python2.7/ sitecustomize.py lib/python2.7/site-packages

如果您想使用 site.py ,在def setEncoding()下:更改第一个If 0:为1:如果:python将使用您的操作系统的语言环境。

如果您喜欢使用 siteCustomize.py ,如果您没有创建它,则可能不存在,只需添加以下行:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

然后您可以以UTF-8格式进行一些中文JSON输出,例如:

name = {"last_name": u"王"}
json.dumps(name, ensure_ascii=False)

您将获得一个UTF-8编码的字符串,而不是\ u逃脱的JSON字符串。

要验证您的默认编码:

print sys.getdefaultencoding()

您应该获取“ UTF-8”或“ UTF-8”以验证您的 site.py.py sitecustomize.py.py 设置。

请注意,您不能在交互式Python控制台上进行 sys.setsss.sendefeaultencoding(“ utf-8”)

Using ensure_ascii=False in json.dumps is the right direction to solve this problem, as pointed out by Martijn. However, this may raise an exception:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 1: ordinal not in range(128)

You need extra settings in either site.py or sitecustomize.py to set your sys.getdefaultencoding() correct. site.py is under lib/python2.7/ and sitecustomize.py is under lib/python2.7/site-packages.

If you want to use site.py, under def setencoding(): change the first if 0: to if 1: so that Python will use your operation system's locale.

If you prefer to use sitecustomize.py, which may not exist if you haven't created it, simply add these lines:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

Then you can do some Chinese JSON output in UTF-8 format, such as:

name = {"last_name": u"王"}
json.dumps(name, ensure_ascii=False)

You will get an UTF-8 encoded string, rather than a \u escaped JSON string.

To verify your default encoding:

print sys.getdefaultencoding()

You should get "utf-8" or "UTF-8" to verify your site.py or sitecustomize.py settings.

Please note that you could not do sys.setdefaultencoding("utf-8") at an interactive Python console.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文