{"id":6035,"date":"2016-07-29T16:10:56","date_gmt":"2016-07-29T08:10:56","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=6035"},"modified":"2016-07-29T16:11:35","modified_gmt":"2016-07-29T08:11:35","slug":"python-json-dump-%e8%be%93%e5%87%ba%e4%b8%ad%e6%96%87","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/6035.html","title":{"rendered":"python json dump \u8f93\u51fa\u4e2d\u6587"},"content":{"rendered":"<p>\u5728\u5199\u722c\u866b\u65f6\uff0c\u4f7f\u7528python\u81ea\u5e26\u7684json\u6a21\u5757\u5c06\u7f51\u9875\u4e2d\u9700\u8981\u7684\u4fe1\u606f\u8f93\u51fa\u4fdd\u5b58\u5230json\u6587\u4ef6\u4e2d\uff0c\u53d1\u73b0json\u6587\u4ef6\u4e2d\u7684\u4e2d\u6587 \u683c\u5f0f\u5168\u662f\u7c7b\u4f3c\u4e8e\u201d\\u5176\\u5b83\u201d\u7684\u5f62\u5f0f\u3002\u53ef\u89c1\u8be5\u6a21\u5757\u5c06\u8f93\u51fa\u7684\u4e2d\u6587\u8fdb\u884c\u4e86 \u7f16\u7801\uff0c\u5c06utf-8\u7801\u76f4\u63a5\u53d8\u6210\u4e86 ascii\u5b57\u7b26\u7684\u5f62\u5f0f\u3002\u6240\u4ee5\u8fd9\u4e9b\u5b57\u7b26\u65e0\u6cd5\u76f4\u63a5\u9605\u8bfb\u3002 \u8981\u76f4\u63a5\u8f93\u51fa\u4e2d\u6587\u9700\u8981\u4f7f\u7528 json.dump()\u51fd\u6570\u7684 ensure_ascii=False\u9009\u9879\u3002\u540c\u65f6\u9700\u8981\u91c7\u7528 codecs\u6a21\u5757\u7684open\u51fd\u6570\uff0c\u6307\u5b9a\u8f93\u51fa\u6587\u4ef6\u7684\u7f16\u7801\u65b9\u5f0f\u3002 \u5b8c\u6574\u7684\u4ee3\u7801\u7247\u6bb5\u5982\u4e0b\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n    # -*-coding:utf-8 -*-\r\n    import json\r\n    import codecs\r\n\r\n    fobj = open(&quot;gaokao.json&quot;,'r')\r\n    wfobj = codecs.open(&quot;formarted_gaokao.json&quot;, 'w', encoding=&quot;utf-8&quot;)\r\n\r\n    universities = json.load(fobj)\r\n    json.dump(universities, wfobj, indent=4, ensure_ascii=False)\r\n\r\n    fobj.close()\r\n    wfobj.close()\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u5199\u722c\u866b\u65f6\uff0c\u4f7f\u7528python\u81ea\u5e26\u7684json\u6a21\u5757\u5c06\u7f51\u9875\u4e2d\u9700\u8981\u7684\u4fe1\u606f\u8f93\u51fa\u4fdd\u5b58\u5230json\u6587\u4ef6\u4e2d\uff0c\u53d1\u73b0json\u6587\u4ef6\u4e2d\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-6035","post","type-post","status-publish","format-standard","hentry","category-skill"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6035","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=6035"}],"version-history":[{"count":2,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6035\/revisions"}],"predecessor-version":[{"id":6037,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/6035\/revisions\/6037"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=6035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=6035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=6035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}