{"id":6664,"date":"2014-09-12T17:18:23","date_gmt":"2014-09-12T09:18:23","guid":{"rendered":"http:\/\/jpuyy.com\/?p=6664"},"modified":"2014-09-15T11:07:23","modified_gmt":"2014-09-15T03:07:23","slug":"python-encode-decode-unicode","status":"publish","type":"post","link":"https:\/\/jpuyy.com\/?p=6664","title":{"rendered":"python encode decode\u7f16\u7801"},"content":{"rendered":"<p>utf8\u7f16\u7801\u7684\u6db5\u4e49<\/p>\n<p>UTF-8 is one of the most commonly used encodings. UTF stands for \u201cUnicode Transformation Format\u201d, and the \u20188\u2019 means that 8-bit numbers are used in the encoding.<\/p>\n<p>\u65e9\u57281968\u5e74\uff0cASCII\u4ee3\u7801\u53d1\u8868\u4e86\uff0c\u4ee3\u8868\u4e860\u5230127\u7684\u5b57\u6bcd\u6570\u5b57\uff0c\u4f46\u4ecd\u8868\u793a\u4e0d\u4e86\u5176\u4ed6\u56fd\u5bb6\u7684\u5b57\u6bcd\uff0c1980s\u4e4b\u540e\uff0c\u5904\u7406\u5668\u53d8\u4e3a8-bit\uff0c\u53d8\u4e3a\u4e860-255\uff0c\u540e\u6765\u4e3a\u4e3a\u4e8616-bit\uff0c\u8bf4\u660e2^16 = 65,536\u3002\u4e4b\u540eutf-8\u51fa\u73b0\u4e86\u3002<\/p>\n<p>\u53ef\u4ee5\u7528type\u6216isinstance\u6765\u5224\u65ad\u53d8\u91cf\u662f\u4ec0\u4e48\u7c7b\u578b<\/p>\n<pre>&gt;&gt;&gt; s = '\u6768'\r\n&gt;&gt;&gt; type(s)\r\n&lt;type 'str'&gt;\r\n&gt;&gt;&gt; isinstance(s, str)\r\nTrue\r\n&gt;&gt;&gt; isinstance(s, unicode)\r\nFalse\r\n<\/pre>\n<p>\u5982\u679c\u524d\u9762\u52a0\u4e00\u4e2au\u7b26\u53f7\u6307\u5b9a\u7528unicode\u7f16\u7801<\/p>\n<pre>&gt;&gt;&gt; a = u'\u6768'\r\n&gt;&gt;&gt; type(a)\r\n&lt;type 'unicode'&gt;\r\n&gt;&gt;&gt; isinstance(a, str)\r\nFalse\r\n&gt;&gt;&gt; isinstance(a, unicode)\r\nTrue\r\n<\/pre>\n<p>python encode unicode\u7f16\u7801<\/p>\n<pre>&gt;&gt;&gt; str = \"\u4e0a\u6d77\"\r\n &gt;&gt;&gt; print str\r\n \u4e0a\u6d77\r\n &gt;&gt;&gt; print data.encode(\"unicode_escape\")\r\n \\\\u4ea4\\\\u6362\\\\u673a\r\n &gt;&gt;&gt; print data.encode(\"raw_unicode_escape\")\r\n \\u4ea4\\u6362\\u673a<\/pre>\n<p>python decode unicode\u7f16\u7801<\/p>\n<pre>&gt;&gt;&gt; data = \"\\u4ea4\\u6362\\u673a\"\r\n&gt;&gt;&gt; type(data)\r\n&lt;type 'str'&gt;\r\n &gt;&gt;&gt; print data.decode('unicode_escape')\r\n \u4ea4\u6362\u673a\r\n \u5f53\u5b57\u7b26\u4e32\u672c\u8eab\u6709\\\u65f6\uff0c\u4f7f\u7528\r\n &gt;&gt;&gt; print data.decode('raw_unicode_escape')\r\n \u4ea4\u6362\u673a<\/pre>\n<p>\u53c2\u8003\u6587\u6863:<\/p>\n<p>https:\/\/docs.python.org\/2\/howto\/unicode.html<\/p>\n","protected":false},"excerpt":{"rendered":"<p>utf8\u7f16\u7801\u7684\u6db5\u4e49 UTF-8 is one of the most commonly used encodings. UTF stands for \u201cUnicode Transformation Format\u201d, and the \u20188\u2019 means that 8-bit numbers are used in the encoding. \u65e9\u57281968\u5e74\uff0cASCII\u4ee3\u7801\u53d1\u8868\u4e86\uff0c\u4ee3\u8868\u4e860\u5230127\u7684\u5b57\u6bcd\u6570\u5b57\uff0c\u4f46\u4ecd\u8868\u793a\u4e0d\u4e86\u5176\u4ed6\u56fd\u5bb6\u7684\u5b57\u6bcd\uff0c1980s\u4e4b\u540e\uff0c\u5904\u7406\u5668\u53d8\u4e3a8-bit\uff0c\u53d8\u4e3a\u4e860-255\uff0c\u540e\u6765\u4e3a\u4e3a\u4e8616-bit\uff0c\u8bf4\u660e2^16 = 65,536\u3002\u4e4b\u540eutf-8\u51fa\u73b0\u4e86\u3002 \u53ef\u4ee5\u7528type\u6216isinstance\u6765\u5224\u65ad\u53d8\u91cf\u662f\u4ec0\u4e48\u7c7b\u578b &gt;&gt;&gt; s = &#8216;\u6768&#8217; &gt;&gt;&gt; type(s) &lt;type &#8216;str&#8217;&gt; &gt;&gt;&gt; isinstance(s, str) True &gt;&gt;&gt; isinstance(s, unicode) False \u5982\u679c\u524d\u9762\u52a0\u4e00\u4e2au\u7b26\u53f7\u6307\u5b9a\u7528unicode\u7f16\u7801 &gt;&gt;&gt; a = u&#8217;\u6768&#8217; &gt;&gt;&gt; type(a) [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[],"class_list":["post-6664","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/6664","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6664"}],"version-history":[{"count":9,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/6664\/revisions"}],"predecessor-version":[{"id":6689,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/6664\/revisions\/6689"}],"wp:attachment":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6664"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6664"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6664"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}