{"id":175293,"date":"2024-05-08T18:48:41","date_gmt":"2024-05-08T10:48:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/175293.html"},"modified":"2024-05-08T18:48:49","modified_gmt":"2024-05-08T10:48:49","slug":"python%e4%b8%ad%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0js%e9%87%8c%e7%9a%84charcodeat%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/175293.html","title":{"rendered":"python\u4e2d\u5982\u4f55\u5b9e\u73b0js\u91cc\u7684charCodeAt()\u65b9\u6cd5"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27053130\/f96dc284-055f-48ba-a809-d76005880228.webp\" alt=\"python\u4e2d\u5982\u4f55\u5b9e\u73b0js\u91cc\u7684charCodeAt()\u65b9\u6cd5\" \/><\/p>\n<p><p>\u5b9e\u73b0JavaScript\u4e2d\u7684<code>charCodeAt()<\/code>\u65b9\u6cd5\u5728Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u5b8c\u6210\uff0c\u4e3b\u8981\u662f <strong>\u5229\u7528<code>ord()<\/code>\u51fd\u6570\u3001\u5185\u7f6e\u7684<code>encode<\/code>\u65b9\u6cd5\u3001\u4ee5\u53ca\u4f7f\u7528\u7b2c\u4e09\u5e93<\/strong>\u3002<code>ord()<\/code>\u51fd\u6570\u662f\u6700\u76f4\u89c2\u548c\u7b80\u5355\u7684\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u83b7\u53d6\u5355\u4e2a\u5b57\u7b26\u7684Unicode\u7801\u70b9\uff0c\u7c7b\u4f3c\u4e8eJavaScript\u4e2d<code>charCodeAt()<\/code>\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><p>\u5728\u6df1\u5165\u4e00\u4e2a\u4f8b\u5b50\u4e4b\u524d\uff0c\u662f\u65f6\u5019\u4e86\u89e3\u4e00\u4e0b<code>ord()<\/code>\u51fd\u6570\u7684\u5de5\u4f5c\u539f\u7406\u3002Python\u7684<code>ord()<\/code>\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\uff08\u957f\u5ea6\u4e3a1\u7684\u5b57\u7b26\u4e32\uff09\u4f5c\u4e3a\u8f93\u5165\uff0c\u5e76\u8fd4\u56de\u8be5\u5b57\u7b26\u5728Unicode\u5b57\u7b26\u96c6\u4e2d\u7684\u5bf9\u5e94\u6570\u503c\u3002\u8fd9\u5728\u6267\u884c\u5b57\u7b26\u7f16\u7801\u64cd\u4f5c\u65f6\u975e\u5e38\u6709\u7528\uff0c\u5c24\u5176\u662f\u5f53\u9700\u8981\u51c6\u786e\u5730\u77e5\u9053\u5b57\u7b26\u7684Unicode\u7801\u70b9\u65f6\u3002\u6bd4\u5982\uff0c<code>ord(&#039;A&#039;)<\/code>\u4f1a\u8fd4\u56de65\uff0c\u8fd9\u662f\u5927\u5199\u5b57\u6bcdA\u5728Unicode\u4e2d\u7684\u7801\u70b9\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>ORD()<\/code>\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u8981\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u5b57\u7b26\u7684Unicode\u7801\u70b9\uff0c\u53ef\u4ee5\u7b80\u5355\u5730\u4f7f\u7528<code>ord()<\/code>\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u5176Unicode\u7801\u70b9\uff0c\u8fd9\u4e0eJavaScript\u7684<code>charCodeAt()<\/code>\u65b9\u6cd5\u76f8\u4f3c\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u4f8b\u5b50\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32<code>str = &quot;Hello&quot;<\/code>, \u8981\u83b7\u53d6\u7b2c\u4e00\u4e2a\u5b57\u7b26<code>&#039;H&#039;<\/code>\u7684Unicode\u7801\u70b9\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0bPython\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">char_code = ord(str[0])<\/p>\n<p>print(char_code) # \u8f93\u51fa 72<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u884c\u4ee3\u7801\u5c06\u8f93\u51fa72\uff0c\u56e0\u4e3a<code>&#039;H&#039;<\/code>\u7684Unicode\u7801\u70b9\u5c31\u662f72\u3002\u8fd9\u663e\u793a\u4e86\u5982\u4f55\u7b80\u5355\u5730\u66ff\u4ee3JavaScript\u7684<code>charCodeAt()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>ENCODE<\/code>\u65b9\u6cd5\u4e0e<code>BYTEARRAY<\/code><\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>encode()<\/code>\u65b9\u6cd5\uff0c\u5b83\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u7f16\u7801\u4e3a\u7279\u5b9a\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u901a\u5e38\u662fUTF-8\uff0c\u7136\u540e\u901a\u8fc7\u8f6c\u6362\u6210<code>bytearray<\/code>\u6765\u83b7\u53d6\u6bcf\u4e2a\u5b57\u7b26\u7684\u6570\u503c\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>\u4f8b\u5b50\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u4f60\u9700\u8981\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u5b57\u7b26\u7684Unicode\u7801\u70b9\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\u6bb5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">str = &quot;Hello&quot;<\/p>\n<p>encoded_str = str.encode(&#039;utf-8&#039;)  # \u7f16\u7801\u4e3aUTF-8<\/p>\n<p>char_codes = bytearray(encoded_str)  # \u8f6c\u6362\u4e3abytearray<\/p>\n<p>for code in char_codes:<\/p>\n<p>    print(code)  # \u5206\u522b\u6253\u5370\u6bcf\u4e2a\u5b57\u7b26\u7684Unicode\u7801\u70b9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u9010\u4e2a\u6253\u5370<code>&quot;Hello&quot;<\/code>\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684Unicode\u7801\u70b9\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u60f3\u8981\u540c\u65f6\u5904\u7406\u5b57\u7b26\u4e32\u4e2d\u591a\u4e2a\u5b57\u7b26\u7801\u70b9\u503c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u5b57\u7b26\u7f16\u7801\u64cd\u4f5c\uff0c\u6216\u8005\u5f53\u5185\u7f6e\u51fd\u6570\u4e0d\u8db3\u4ee5\u6ee1\u8db3\u9700\u6c42\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u7b2c\u4e09\u65b9Python\u5e93\uff0c\u6bd4\u5982<code>chardet<\/code>\u6216<code>unicodedata<\/code>\u5e93\u3002<\/p>\n<\/p>\n<ul>\n<li>\n<p><strong>unicodedata\u5e93\u5b9e\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><p><code>unicodedata<\/code>\u5e93\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u5904\u7406Unicode\u5b57\u7b26\u7684\u65b9\u6cd5\uff0c\u5305\u62ec\u67e5\u627e\u5b57\u7b26\u7684\u540d\u79f0\u3001\u5206\u7c7b\u3001\u6570\u503c\u5c5e\u6027\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unicodedata<\/p>\n<p>str = &quot;Hello&quot;<\/p>\n<p>char = str[0]  # \u83b7\u53d6\u7b2c\u4e00\u4e2a\u5b57\u7b26<\/p>\n<p>unicode_point = unicodedata.numeric(char, default=ord(char))<\/p>\n<p>print(unicode_point)  # \u6253\u5370Unicode\u7801\u70b9<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u867d\u7136\u8fd9\u4e2a\u4f8b\u5b50\u548c\u76f4\u63a5\u4f7f\u7528<code>ord()<\/code>\u51fd\u6570\u7c7b\u4f3c\uff0c<code>unicodedata<\/code>\u5e93\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u5176\u4ed6\u7528\u4e8e\u5904\u7406Unicode\u7684\u9ad8\u7ea7\u529f\u80fd\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<p><h3>\u56db\u3001\u6bd4\u8f83\u5404\u79cd\u65b9\u6cd5\u7684\u9002\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p><strong>\u4e0d\u540c\u7684\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002<\/strong><\/p>\n<\/p>\n<ul>\n<li>\n<p>\u4f7f\u7528<code>ord()<\/code>\u51fd\u6570\uff1a\u6700\u9002\u5408\u5feb\u901f\u7b80\u5355\u83b7\u53d6\u5355\u4e2a\u5b57\u7b26\u7684Unicode\u7801\u70b9\u3002\u8fd9\u662f\u4e00\u79cd\u975e\u5e38\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u57fa\u672c\u9700\u6c42\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528<code>encode<\/code>\u65b9\u6cd5\u4e0e<code>bytearray<\/code>\uff1a\u5f53\u9700\u8981\u5904\u7406\u5b57\u7b26\u4e32\u4e2d\u7684\u591a\u4e2a\u5b57\u7b26\uff0c\u5e76\u4e14\u60f3\u8981\u83b7\u53d6\u5b83\u4eec\u7f16\u7801\u540e\u7684\u6570\u503c\u65f6\uff0c\u8fd9\u79cd\u65b9\u6cd5\u662f\u975e\u5e38\u6709\u6548\u7684\u3002\u5c24\u5176\u5728\u5904\u7406\u975eASCII\u5b57\u7b26\u65f6\u5c55\u73b0\u51fa\u5176\u5f3a\u5927\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p>\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\uff1a\u5f53\u5185\u7f6e\u65b9\u6cd5\u4e0d\u8db3\u4ee5\u6ee1\u8db3\u4f60\u7684\u9700\u6c42\u65f6\uff0c\u7b2c\u4e09\u65b9\u5e93\u5982<code>unicodedata<\/code>\u63d0\u4f9b\u4e86\u8fdb\u4e00\u6b65\u5904\u7406Unicode\u5b57\u7b26\u7684\u9ad8\u7ea7\u529f\u80fd\uff0c\u9002\u5408\u4e8e\u9700\u8981\u6df1\u5165\u5b57\u7b26\u5c5e\u6027\u7684\u5e94\u7528\u3002<\/p>\n<\/p>\n<\/li>\n<\/ul>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u5b57\u7b26\u7684Unicode\u7f16\u7801\uff1f<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684ord()\u51fd\u6570\u6765\u83b7\u53d6\u5b57\u7b26\u7684Unicode\u7f16\u7801\u3002ord()\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4f5c\u4e3a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u5bf9\u5e94\u7684Unicode\u7f16\u7801\u503c\u3002<\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre><code class=\"language-python\">char = &#039;A&#039;\nunicode_value = ord(char)\nprint(unicode_value)\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a65<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u7c7b\u4f3c\u4e8eJavaScript\u7684charCodeAt()\u65b9\u6cd5\uff1f<\/strong><\/p>\n<p>\u5728Python\u4e2d\uff0c\u6ca1\u6709\u76f4\u63a5\u63d0\u4f9b\u7c7b\u4f3c\u4e8eJavaScript\u7684charCodeAt()\u65b9\u6cd5\u3002\u4f46\u662f\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5148\u5c06\u5b57\u7b26\u8f6c\u6362\u4e3aUnicode\u7f16\u7801\uff0c\u7136\u540e\u4f7f\u7528str()\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u6700\u540e\u518d\u4f7f\u7528int()\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u5f97\u5230\u5b57\u7b26\u7684Unicode\u7f16\u7801\u503c\u3002<\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre><code class=\"language-python\">char = &#039;A&#039;\nunicode_value = int(str(ord(char)))\nprint(unicode_value)\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a65<\/p>\n<p><strong>\u5982\u4f55\u904d\u5386\u5b57\u7b26\u4e32\u5e76\u83b7\u53d6\u6bcf\u4e2a\u5b57\u7b26\u7684Unicode\u7f16\u7801\uff1f<\/strong><\/p>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u6765\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u4f7f\u7528ord()\u51fd\u6570\u83b7\u53d6\u6bcf\u4e2a\u5b57\u7b26\u7684Unicode\u7f16\u7801\u503c\u3002<\/p>\n<p><strong>\u793a\u4f8b\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre><code class=\"language-python\">string = &#039;Hello&#039;\nfor char in string:\n    unicode_value = ord(char)\n    print(f&quot;The Unicode value of {char} is {unicode_value}&quot;)\n<\/code><\/pre>\n<p>\u8f93\u51fa\u7ed3\u679c\uff1a<\/p>\n<pre><code>The Unicode value of H is 72\nThe Unicode value of e is 101\nThe Unicode value of l is 108\nThe Unicode value of l is 108\nThe Unicode value of o is 111\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5b9e\u73b0JavaScript\u4e2d\u7684charCodeAt()\u65b9\u6cd5\u5728Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u5b8c\u6210\uff0c\u4e3b\u8981\u662f \u5229\u7528or [&hellip;]","protected":false},"author":3,"featured_media":175311,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/175293"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=175293"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/175293\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/175311"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=175293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=175293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=175293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}