擅长:python、mysql、java
<p>在python2中,它工作;但是在python3中,<code>print</code>将输出如下:</p>
<pre><code>>>> b'{\n "content": "\xe4\xbd\xa0\xe5\xa5\xbd"\n}'
</code></pre>
<p>不要使用<code>encode('utf8')</code>:</p>
<pre><code>>>> print(json.dumps(data, indent=1, ensure_ascii=False))
{
"content": "你好"
}
</code></pre>
<p>或者使用<code>sys.stdout.buffer.write</code>而不是<code>print</code>:</p>
<pre><code>>>> import sys
>>> import json
>>> data = {'content': u'\u4f60\u597d'}
>>> sys.stdout.buffer.write(json.dumps(data, indent=1,
ensure_ascii=False).encode('utf8') + b'\n')
{
"content": "你好"
}
</code></pre>
<p>见<a href="https://stackoverflow.com/a/3603160">Write UTF-8 to stdout, regardless of the console's encoding</a></p>