{"id":4466,"date":"2019-06-14T13:36:42","date_gmt":"2019-06-14T05:36:42","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4466"},"modified":"2019-06-16T21:27:17","modified_gmt":"2019-06-16T13:27:17","slug":"python%e4%b8%ad%e7%9a%84%e5%ad%97%e8%8a%82%e5%92%8c%e5%ad%97%e7%ac%a6%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4466.html","title":{"rendered":"Python\u4e2d\u7684\u5b57\u8282\u548c\u5b57\u7b26\u4e32"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u6574\u7406\u603b\u7ed3\u4e00\u4e0b\u6700\u8fd1\u9047\u5230\u6bd4\u8f83\u591a\u7684Python\u4e2d\u5b57\u8282\u548c\u5b57\u7b26\u4e32\u4e4b\u95f4\u7684\u5c0f\u77e5\u8bc6\u70b9\uff0c\u65b9\u4fbf\u4ee5\u540e\u5feb\u901f\u53c2\u8003\u3001\u5b66\u4e60\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<h6 class=\"wp-block-heading\"><strong>Python 3\u4e2d\u7684bytes\u548cstr\u7c7b\u578b<\/strong><\/h6>\n\n\n\n<p class=\"has-text-color has-vivid-red-color\"><strong>Python 3\u6700\u91cd\u8981\u7684\u65b0\u7279\u6027\u5927\u6982\u8981\u7b97\u662f\u5bf9\u6587\u672c\u548c\u4e8c\u8fdb\u5236\u6570\u636e\u4f5c\u4e86\u66f4\u4e3a\u6e05\u6670\u7684\u533a\u5206\u3002\u6587\u672c\u603b\u662fUnicode\uff0c\u7531str\u7c7b\u578b\u8868\u793a\uff0c\u4e8c\u8fdb\u5236\u6570\u636e\u5219\u7531bytes\u7c7b\u578b\u8868\u793a\u3002Python 3\u4e0d\u4f1a\u4ee5\u4efb\u610f\u9690\u5f0f\u7684\u65b9\u5f0f\u6df7\u7528str\u548cbytes\uff0c\u6b63\u662f\u8fd9\u4f7f\u5f97\u4e24\u8005\u7684\u533a\u5206\u7279\u522b\u6e05\u6670\u3002<\/strong>\u4f60\u4e0d\u80fd\u62fc\u63a5\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u5305\uff0c\u4e5f\u65e0\u6cd5\u5728\u5b57\u8282\u5305\u91cc\u641c\u7d22\u5b57\u7b26\u4e32\uff08\u53cd\u4e4b\u4ea6\u7136\uff09\uff0c\u4e5f\u4e0d\u80fd\u5c06\u5b57\u7b26\u4e32\u4f20\u5165\u53c2\u6570\u4e3a\u5b57\u8282\u5305\u7684\u51fd\u6570\uff08\u53cd\u4e4b\u4ea6\u7136\uff09\u3002\u8fd9\u662f\u4ef6\u597d\u4e8b\u3002<\/p>\n\n\n\n<p class=\"has-background has-very-light-gray-background-color\"><strong>\u5b57\u7b26\u4e32\u53ef\u4ee5\u7f16\u7801encode()\u6210\u5b57\u8282\u5305<\/strong>\uff0c\u800c<strong>\u5b57\u8282\u5305\u53ef\u4ee5\u89e3\u7801decode()\u6210\u5b57\u7b26\u4e32<\/strong>\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code># Python 3 \u4ea4\u4e92\u7ec8\u7aef\n>>> website = 'https:\/\/ixyzero.com\/blog\/'\n>>> type(website)\n&lt;class 'str'>\n>>> website\n'https:\/\/ixyzero.com\/blog\/'\n\n# \u5c06 string \u8f6c\u6362\u6210 bytes \uff0c\u4f7f\u7528 .encode() \u65b9\u6cd5\n>>> website_bytes_utf8 = website.encode(encoding=\"utf-8\")\n>>> type(website_bytes_utf8)\n&lt;class 'bytes'>\n>>> website_bytes_utf8\nb'https:\/\/ixyzero.com\/blog\/'\n\n# \u5c06 bytes \u8f6c\u6362\u6210 string \uff0c\u4f7f\u7528 .decode() \u65b9\u6cd5\n>>> website_string = website_bytes_utf8.decode()\n>>> type(website_string)\n&lt;class 'str'>\n>>> website_string\n'https:\/\/ixyzero.com\/blog\/'\n>>><\/code><\/pre>\n\n\n\n<p>&amp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>>>> b\"abcde\"\nb'abcde'\n\n# utf-8 is used here because it is a very common encoding, but you\n# need to use the encoding your data is actually in.\n>>> b\"abcde\".decode(\"utf-8\") \n'abcde'<\/code><\/pre>\n\n\n\n<p><strong>Python\u4e2d\u5982\u4f55\u83b7\u53d6\u67d0\u4e2a\u5b57\u7b26\u4e32\u7684\u300c\u5b57\u8282\u957f\u5ea6\u300d\uff1f<\/strong>(python get string byte length)<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>def utf8len(s):\n    return len(s.encode('utf-8'))<\/code><\/pre>\n\n\n\n<p>&amp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code># getsizeof(object, default) -> int\n# Return the size of object in bytes.\n# \u8fd9\u79cd\u65b9\u6cd5\u83b7\u53d6\u7684\u662fPython\u5bf9\u8c61\u7684bytes\u5927\u5c0f\uff0c\u548c\u6211\u4eec\u671f\u671b\u7684\u6548\u679c\u5e76\u4e0d\u76f8\u540c\uff0c\u800c\u4e14\u4e0d\u540c\u7248\u672c\u3001\u7cfb\u7edf\u7684\u503c\u4e5f\u5e76\u4e0d\u4e00\u81f4\nimport sys\nsys.getsizeof(s)\n\n>>> len(\"hello\".encode(\"utf8\"))\n5\n>>> len(\"\u4f60\u597d\".encode(\"utf8\"))\n6\n\n####\nPython 2.7.10 (default, Aug 17 2018, 19:45:58)\n[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)] on darwin\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n>>> import sys\n>>> reload(sys)\n&lt;module 'sys' (built-in)>\n>>> sys.setdefaultencoding('utf-8')\n>>>\n>>> utf8len('\u4f60\u597d')\n6\n>>> utf8len('hello')\n5\n>>> sys.getsizeof('\u4f60\u597d')\n43\n>>>\n>>> sys.getsizeof('hello')\n42\n>>>\n\n####\nPython 3.6.5 (default, Apr 10 2018, 20:17:30)\n[GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.1)] on darwin\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n>>> import sys\n>>> sys.getsizeof('\u4f60\u597d')\n78\n>>> sys.getsizeof('hello')\n54\n>>>\n>>> utf8len('\u4f60\u597d')\n6\n>>> utf8len('hello')\n5\n>>><\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"http:\/\/www.ituring.com.cn\/article\/1116\">Python 3\u7684bytes\/str\u4e4b\u522b<\/a><\/li><li><a href=\"http:\/\/eli.thegreenplace.net\/2012\/01\/30\/the-bytesstr-dichotomy-in-python-3\/\">http:\/\/eli.thegreenplace.net\/2012\/01\/30\/the-bytesstr-dichotomy-in-python-3\/<\/a><\/li><li><a href=\"https:\/\/blog.csdn.net\/lyb3b3b\/article\/details\/74993327\">Python3\u4e2d\u7684bytes\u548cstr\u7c7b\u578b<\/a><\/li><li><strong><a href=\"https:\/\/www.cnblogs.com\/txw1958\/archive\/2012\/08\/31\/python3-bytes-string.html\">python3\u4e2dbytes\u4e0estring\u7684\u4e92\u76f8\u8f6c\u6362<\/a><\/strong><\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/606191\/convert-bytes-to-a-string\">Convert bytes to a string?<\/a><\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/30686701\/python-get-size-of-string-in-bytes\">https:\/\/stackoverflow.com\/questions\/30686701\/python-get-size-of-string-in-bytes<\/a><\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/4013230\/how-many-bytes-does-a-string-have\">How many bytes does a string have<\/a><\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/4967580\/how-to-get-the-size-of-a-string-in-python\">How to get the size of a string in Python?<\/a><\/li><li><a href=\"https:\/\/docs.python.org\/3\/library\/sys.html#sys.getsizeof\">https:\/\/docs.python.org\/3\/library\/sys.html#sys.getsizeof<\/a><br><a href=\"https:\/\/docs.python.org\/3\/library\/functions.html#len\">https:\/\/docs.python.org\/3\/library\/functions.html#len<\/a><\/li><\/ul>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u6574\u7406\u603b\u7ed3\u4e00\u4e0b\u6700\u8fd1\u9047\u5230\u6bd4\u8f83\u591a\u7684Python\u4e2d\u5b57\u8282\u548c\u5b57\u7b26\u4e32\u4e4b\u95f4\u7684\u5c0f\u77e5\u8bc6\u70b9\uff0c\u65b9\u4fbf\u4ee5\u540e\u5feb\u901f\u53c2\u8003 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7],"tags":[1424,1426,1425,8,1427,558],"class_list":["post-4466","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","tag-bytes","tag-decode","tag-encode","tag-python","tag-python3","tag-string"],"views":3993,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4466"}],"version-history":[{"count":3,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4466\/revisions"}],"predecessor-version":[{"id":4481,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4466\/revisions\/4481"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}