{"id":3288,"date":"2025-06-02T15:22:27","date_gmt":"2025-06-04T08:20:26","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3288"},"modified":"2025-06-04T15:22:27","modified_gmt":"2025-06-04T08:20:26","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84bytes%e5%87%bd%e6%95%b8-2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84bytes%e5%87%bd%e6%95%b8-2\/","title":{"rendered":"\u6df1\u5165\u63a2\u8a0e Python \u7684 bytes() \u51fd\u5f0f\uff1a\u5f9e\u57fa\u672c\u7528\u6cd5\u5230\u5be6\u969b\u61c9\u7528\uff082025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, bytes(), bytes \u51fd\u5f0f, Python \u6559\u5b78, \u5b57\u4e32\u8f49 bytes\"><\/p>\n<h1>\u6df1\u5165\u63a2\u8a0e Python \u7684 bytes() \u51fd\u5f0f\uff1a\u5f9e\u57fa\u672c\u7528\u6cd5\u5230\u5be6\u969b\u61c9\u7528\uff082025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff09<\/h1>\n<p>\u5728 Python \u4e2d\uff0c<strong>bytes()<\/strong> \u51fd\u5f0f\u662f\u4e00\u500b\u975e\u5e38\u91cd\u8981\u7684\u5de5\u5177\uff0c\u7528\u4f86\u5c07\u5b57\u4e32\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u7269\u4ef6\u8f49\u63db\u70ba bytes \u7269\u4ef6\u3002\u9019\u4e9b bytes \u7269\u4ef6\u662f\u4e0d\u53ef\u8b8a\u7684\u5e8f\u5217\uff0c\u9069\u5408\u7528\u4f86\u5132\u5b58\u539f\u59cb\u7684\u4e8c\u9032\u4f4d\u8cc7\u6599\uff0c\u4f8b\u5982\u5716\u7247\u6216\u97f3\u6a02\u6a94\u6848\uff0c\u4e26\u53ef\u7528\u65bc\u7db2\u8def\u50b3\u8f38\u3002<\/p>\n<p>### bytes() \u51fd\u5f0f\u7684\u57fa\u672c\u7528\u6cd5<\/p>\n<p>`bytes()` \u51fd\u5f0f\u53ef\u4ee5\u63a5\u53d7\u4e00\u500b\u6216\u5169\u500b\u53c3\u6578\u3002\u82e5\u53ea\u6709\u4e00\u500b\u53c3\u6578\u6642\uff0c\u5b83\u53ef\u4ee5\u662f\u5b57\u4e32\u3001\u6574\u6578\u3001\u5143\u7d44\u6216\u5176\u4ed6\u53ef\u8fed\u4ee3\u7269\u4ef6\u3002\u4ee5\u4e0b\u662f\u5e7e\u500b\u793a\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5c07\u5b57\u4e32\u8f49\u63db\u6210 bytes \u7269\u4ef6<br \/>\nx = bytes(&#8220;Hello World&#8221;, encoding=&#8221;utf-8&#8243;)<\/p>\n<p># \u5c07\u6574\u6578\u8f49\u63db\u6210 bytes \u7269\u4ef6<br \/>\nx = bytes([0x41, 0x42, 0x43])  # \u7522\u751f b&#8217;ABC&#8217;<\/p>\n<p># \u5c07\u5143\u7d44\u8f49\u63db\u6210 bytes \u7269\u4ef6<br \/>\nx = bytes((0x41, 0x42, 0x43))  # \u7522\u751f b&#8217;ABC&#8217;<\/p>\n<p># \u5c07\u53ef\u8fed\u4ee3\u7269\u4ef6\u8f49\u63db\u6210 bytes \u7269\u4ef6<br \/>\nx = bytes([i for i in range(10)])  # \u7522\u751f b&#8217;\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\x09&#8242;<br \/>\n&#8220;`<\/p>\n<p>\u82e5\u6709\u5169\u500b\u53c3\u6578\uff0c\u7b2c\u4e00\u500b\u53c3\u6578\u70ba\u8981\u8f49\u63db\u7684\u7269\u4ef6\uff0c\u7b2c\u4e8c\u500b\u53c3\u6578\u70ba\u7de8\u78bc\u65b9\u5f0f\uff1a<\/p>\n<p>&#8220;`python<br \/>\nx = bytes(&#8220;Hello World&#8221;, encoding=&#8221;utf-8&#8243;)  # \u7522\u751f b&#8217;Hello World&#8217;<br \/>\n&#8220;`<\/p>\n<p>### bytes() \u51fd\u5f0f\u7684\u5be6\u969b\u61c9\u7528<\/p>\n<p>`bytes()` \u51fd\u5f0f\u7684\u61c9\u7528\u975e\u5e38\u5ee3\u6cdb\uff0c\u4ee5\u4e0b\u662f\u5e7e\u500b\u5be6\u969b\u7684\u4f7f\u7528\u5834\u666f\uff1a<\/p>\n<p>1. **\u5132\u5b58\u539f\u59cb\u7684\u4e8c\u9032\u4f4d\u8cc7\u6599**\uff1a\u53ef\u4ee5\u7528\u4f86\u8655\u7406\u5716\u7247\u3001\u97f3\u6a02\u6a94\u6848\u7b49\u3002<br \/>\n2. **\u7db2\u8def\u50b3\u8f38**\uff1a\u5c07 bytes \u7269\u4ef6\u767c\u9001\u5230\u7db2\u8def\u4e0a\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5c07\u5716\u7247\u8f49\u63db\u70ba bytes \u7269\u4ef6\u4e26\u9032\u884c\u50b3\u8f38\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5c07\u5716\u7247\u8f49\u63db\u6210 bytes \u7269\u4ef6<br \/>\nwith open(&#8220;image.jpg&#8221;, &#8220;rb&#8221;) as f:<br \/>\n    x = f.read()<\/p>\n<p># \u5c07 bytes \u7269\u4ef6\u50b3\u8f38\u5230\u7db2\u8def<br \/>\ns.send(x)<br \/>\n&#8220;`<\/p>\n<p>### \u5b57\u4e32\u7684\u7de8\u78bc\u8207\u89e3\u78bc<\/p>\n<p>`bytes()` \u51fd\u5f0f\u4e5f\u53ef\u4ee5\u7528\u4f86\u9032\u884c\u5b57\u4e32\u7684\u7de8\u78bc\u548c\u89e3\u78bc\u64cd\u4f5c\uff1a<\/p>\n<p>&#8220;`python<br \/>\n# \u5c07\u5b57\u4e32\u7de8\u78bc\u6210 bytes \u7269\u4ef6<br \/>\nx = bytes(&#8220;Hello World&#8221;, encoding=&#8221;utf-8&#8243;)<\/p>\n<p># \u5c07 bytes \u7269\u4ef6\u89e3\u78bc\u6210\u5b57\u4e32<br \/>\nx = x.decode(&#8220;utf-8&#8221;)  # \u8fd4\u56de &#8216;Hello World&#8217;<br \/>\n&#8220;`<\/p>\n<p>\u900f\u904e\u9019\u4e9b\u64cd\u4f5c\uff0c\u60a8\u53ef\u4ee5\u8f15\u9b06\u5730\u5728 Python \u4e2d\u8655\u7406\u5b57\u4e32\u8207\u4e8c\u9032\u4f4d\u8cc7\u6599\u4e4b\u9593\u7684\u8f49\u63db\uff0c\u4e26\u5728\u5132\u5b58\u548c\u50b3\u8f38\u6642\u4fdd\u6301\u8cc7\u6599\u7684\u5b8c\u6574\u6027\u3002<\/p>\n<p>### \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u4f7f\u7528 `bytes()` \u51fd\u5f0f\u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u5305\u62ec\uff1a<\/p>\n<p>&#8211; **\u7de8\u78bc\u932f\u8aa4**\uff1a\u78ba\u4fdd\u6240\u63d0\u4f9b\u7684\u7de8\u78bc\u65b9\u5f0f\u662f\u6b63\u78ba\u7684\uff0c\u5426\u5247\u6703\u5f15\u767c `LookupError`\u3002<br \/>\n&#8211; **\u8cc7\u6599\u985e\u578b\u932f\u8aa4**\uff1a\u78ba\u4fdd\u50b3\u5165\u7684\u8cc7\u6599\u985e\u578b\u662f\u652f\u63f4\u7684\uff0c\u5426\u5247\u6703\u5f15\u767c `TypeError`\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>\u60a8\u53ef\u4ee5\u5c07 `bytes()` \u51fd\u5f0f\u8207\u5176\u4ed6 Python \u6a21\u7d44\u7d50\u5408\u4f7f\u7528\uff0c\u9032\u884c\u66f4\u8907\u96dc\u7684\u8cc7\u6599\u8655\u7406\uff0c\u4f8b\u5982\u4f7f\u7528 `struct` \u6a21\u7d44\u4f86\u89e3\u6790\u4e8c\u9032\u4f4d\u8cc7\u6599\uff0c\u6216\u5c07 bytes \u7269\u4ef6\u5b58\u5132\u5230\u8cc7\u6599\u5eab\u4e2d\u3002\u82e5\u60f3\u6df1\u5165\u4e86\u89e3 Python \u7684\u8cc7\u6599\u8655\u7406\uff0c\u8acb\u53c3\u8003 [\u9019\u88e1\u7684\u9032\u968e\u6559\u5b78](https:\/\/vocus.cc) \u3002<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: bytes() \u8207 bytearray \u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f**<br \/>\nA1: `bytes()` \u7522\u751f\u4e0d\u53ef\u8b8a\u7684 bytes \u7269\u4ef6\uff0c\u800c `bytearray` \u5247\u662f\u53ef\u8b8a\u7684\uff0c\u9069\u5408\u5728\u9700\u8981\u4fee\u6539\u8cc7\u6599\u7684\u60c5\u6cc1\u4e0b\u4f7f\u7528\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u5c07 bytes \u7269\u4ef6\u8f49\u63db\u56de\u5b57\u4e32\uff1f**<br \/>\nA2: \u4f7f\u7528 `decode()` \u65b9\u6cd5\u53ef\u4ee5\u5c07 bytes \u7269\u4ef6\u8f49\u63db\u56de\u5b57\u4e32\uff0c\u4f8b\u5982 `x.decode(&#8220;utf-8&#8221;)`\u3002<\/p>\n<p>**Q3: \u4f7f\u7528 bytes() \u7522\u751f\u7684\u8cc7\u6599\u5982\u4f55\u50b3\u9001\u5230\u7db2\u8def\uff1f**<br \/>\nA3: \u60a8\u53ef\u4ee5\u4f7f\u7528 socket \u6a21\u7d44\u7684 `send()` \u65b9\u6cd5\u5c07 bytes \u7269\u4ef6\u767c\u9001\u81f3\u7db2\u8def\u9023\u63a5\u3002<\/p>\n<p>&#8212;<\/p>\n<p>\u9019\u6a23\u7684\u512a\u5316\u61c9\u8a72\u80fd\u5920\u5e6b\u52a9\u63d0\u5347\u6587\u7ae0\u7684\u53ef\u8b80\u6027\u8207 SEO \u6548\u679c\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3Python\u4e2d\u7684Bytes()\u51fd\u6578\uff0c\u5b83\u53ef\u4ee5\u5c07\u5b57\u7b26\u4e32\u8f49\u63db\u70ba\u5b57\u7bc0\u5e8f\u5217\uff0c\u4e26\u63d0\u4f9b\u4e86\u4e00\u7a2e\u65b9\u4fbf\u7684\u65b9\u6cd5\u4f86\u64cd\u4f5c\u5b57\u7bc0\u5e8f\u5217\u3002\u672c\u6587\u5c07\u4ecb\u7d39Bytes()\u51fd\u6578\u7684\u7528\u6cd5\uff0c\u4ee5\u53ca\u5b83\u5982\u4f55\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u597d\u5730\u64cd\u4f5c\u5b57\u7bc0\u5e8f\u5217\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3288","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-R2","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3288"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3288\/revisions"}],"predecessor-version":[{"id":12877,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3288\/revisions\/12877"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}