{"id":179826,"date":"2022-04-17T08:47:28","date_gmt":"2022-04-17T00:47:28","guid":{"rendered":"http:\/\/www.idc.net\/help\/179826\/"},"modified":"2022-04-17T08:47:28","modified_gmt":"2022-04-17T00:47:28","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91python%e6%95%99%e7%a8%8b%ef%bc%9apython-json-loads%e5%a4%b1%e8%b4%a5%e6%80%8e%e4%b9%88%e5%8a%9e","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/179826\/","title":{"rendered":"\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1apython json.loads\u5931\u8d25\u600e\u4e48\u529e"},"content":{"rendered":"<p><strong><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/article\/000\/000\/003\/5dbf9e4cd7366961.jpg\" class=\"aligncenter\"><\/strong><\/p>\n<p><strong>json.loads Python\u9519\u8bef\uff1a<\/strong><\/p>\n<p><strong>1\u3001'utf8' codec can't decode byte ...<\/strong><\/p>\n<p>\u7531\u4e8e\u9700\u6c42\uff0c\u8981\u7528python\u8bfb\u53d6\u7f51\u9875\u8fd4\u56dejson\uff0c\u5e76\u53d6\u5f97\u5176\u4e2d\u7684\u6570\u636e\u4f46\u662f\u5374\u9047\u5230\u4ee5\u4e0a\u7f16\u7801\u7684\u95ee\u9898\u3002<\/p>\n<p>\u7ec8\u4e8e\u627e\u5230\u4e86\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<p>\u6211\u4eec\u53ea\u9700\u8981\u5bf9\u5b57\u7b26\u4e32\u8fdb\u884cunicode\u5316\uff0c\u5ffd\u7565\u9519\u8bef\u5c31\u884c\u4e86\u3002<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>str2&nbsp;=&nbsp;unicode(&nbsp;str,&nbsp;errors='ignore')<\/pre>\n<p>\u4f46\u662f\u8981\u6ce8\u610f\uff0c\u8fd9\u91cc\u7684str\u5fc5\u987b\u4e3a\u5b57\u7b26\u4e32\uff0c\u5904\u7406\u4e4b\u540e\u518d\u9001\u5165json.load\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<pre>data&nbsp;=&nbsp;json.loads(str2)<\/pre>\n<p>unicode\u5316+\u5ffd\u7565\u9519\u8bef\u771f\u7ed9\u529b\u3002<\/p>\n<p><strong>2\u3001ValueError: Invalid \\escape: line 1 column 35442 (char 35441)<\/strong><\/p>\n<p>\u95ee\u9898\u5728\u4e8e\u7f16\u7801\u4e2d\u662f\\xa0\u4e4b\u7c7b\u7684\uff0c\u5f53\u9047\u5230\u6709\u4e9b\u4e0d\u7528\u8f6c\u4e49\u7684\\http\u4e4b\u7c7b\u7684\uff0c\u5219\u4f1a\u51fa\u73b0\u4ee5\u4e0a\u9519\u8bef\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848\u5982\u4e0b\uff1a<\/p>\n<pre>import&nbsp;re&nbsp;&nbsp;\nregex&nbsp;=&nbsp;re.compile(r'\\\\(?![\/u\"])')&nbsp;&nbsp;\nfixed&nbsp;=&nbsp;regex.sub(r\"\\\\\\\\\",&nbsp;original)<\/pre>\n<p>\u4f17\u591apython\u57f9\u8bad\u89c6\u9891\uff0c\u5c3d\u5728python\u5b66\u4e60\u7f51\uff0c\u6b22\u8fce\u5728\u7ebf\u5b66\u4e60\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>json.loads Python\u9519\u8bef\uff1a 1\u3001'utf8' codec can't decode byte . [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":179827,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-179826","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/179826","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=179826"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/179826\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/179827"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=179826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=179826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=179826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}