最新消息:20210917 已从crifan.com换到crifan.org

【已解决】python中把dict的json输出到文件且带缩进和不要unicode的uxxxx

JSON crifan 3978浏览 0评论

折腾:

【记录】用VSCode开发和调试Python

期间,需要把一个dict的json,输出到文件中,且:

带缩进的,好看

不要\uxxx,要encode后的中文

然后再去把json写入到文件,且要格式化带缩进的效果:

<code>with open("currentJson.json", 'w') as tmpFp:
    json.dump(currentJson, tmpFp, indent=2, encoding="utf-8")
</code>

结果输出中的内容,带\uxxxx而不是编码后的中文:

<code>{
  "pluginsConfig": {
    "toolbar-button": {
      "url": "http://book.crifan.org/books/youdao_note_summary/pdf/youdao_note_summary.pdf"
    }, 
    "sitemap-general": {
      "prefix": "https://book.crifan.org/gitbook/youdao_note_summary/website/"
    }, 
    "github-buttons": {
      "buttons": [
        {
          "repo": "youdao_note_summary"
        }
      ]
    }
  }, 
  "description": "\u603b\u7ed3\u4e4b\u524d\u4f7f\u7528\u8fc7\u6709\u9053\u4e91\u7b14\u8bb0\u548c\u6709\u9053\u4e91\u534f\u4f5c\u7684\u5fc3\u5f97\u4f9b\u53c2\u8003", 
  "title": "\u6709\u9053\u4e91\u7b14\u8bb0\u548c\u4e91\u534f\u4f5c\u4f7f\u7528\u603b\u7ed3"
}
</code>

试了试:

<code># json.dump(currentJson, tmpFp, indent=2)
# currentJsonUtf8 = currentJson.encode("utf-8")
# json.dump(currentJsonUtf8, tmpFp, indent=2)
</code>

问题依旧。

python dict to file without unicode

Python Dict and File  |  Python Education  |  Google Developers

Suppress the u’prefix indicating unicode’ in python strings – Stack Overflow

python – How to get string objects instead of Unicode from JSON? – Stack Overflow

<code>currentJsonDump = json.dumps(currentJson, ensure_ascii=False)
json.dump(currentJsonDump, tmpFp, indent=2, encoding="utf-8")
</code>

问题依旧。

难道非要:

<code>&gt;&gt;&gt; nl = json.loads(js)
&gt;&gt;&gt; nl
[u'a', u'b']
&gt;&gt;&gt; nl = [s.encode('utf-8') for s in nl]
&gt;&gt;&gt; nl
['a', 'b']
</code>

?

python json dump encoded string

python – Saving utf-8 texts in json.dumps as UTF8, not as \u escape sequence – Stack Overflow

dump后再去调用encode utf-8

<code>currentJsonDump = json.dumps(currentJson, ensure_ascii=False)
print("type(currentJsonDump)", type(currentJsonDump)) #('type(currentJsonDump)', &lt;type 'unicode'&gt;)
currentJsonUtf8 = currentJsonDump.encode("utf-8")
json.dump(currentJsonUtf8, tmpFp, indent=2, encoding="utf-8")
</code>

问题依旧。

<code>import codecs

# with open("currentJson.json", 'w') as tmpFp:
with codecs.open("currentJson.json", 'w', encoding="utf-8") as tmpFp:
    # json.dump(currentJson, tmpFp, indent=2, encoding="utf-8")
    # json.dump(currentJson, tmpFp, indent=2)
    # currentJsonUtf8 = currentJson.encode("utf-8")
    # json.dump(currentJsonUtf8, tmpFp, indent=2)
    currentJsonDump = json.dumps(currentJson, ensure_ascii=False)
    print("type(currentJsonDump)", type(currentJsonDump)) #('type(currentJsonDump)', &lt;type 'unicode'&gt;)
    currentJsonUtf8 = currentJsonDump.encode("utf-8")
    print("type(currentJsonUtf8)", type(currentJsonUtf8))
    json.dump(currentJsonUtf8, tmpFp, indent=2, encoding="utf-8")
</code>

问题依旧。

【总结】

最后用:

<code>import codecs

with codecs.open("currentJson.json", 'w', encoding="utf-8") as tmpFp:
    json.dump(currentJson, tmpFp, indent=2, ensure_ascii=False)
</code>

终于输出了中文了:

转载请注明:在路上 » 【已解决】python中把dict的json输出到文件且带缩进和不要unicode的uxxxx

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
88 queries in 0.181 seconds, using 20.09MB memory