{"id":1135339,"date":"2025-01-08T21:24:21","date_gmt":"2025-01-08T13:24:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1135339.html"},"modified":"2025-01-08T21:24:24","modified_gmt":"2025-01-08T13:24:24","slug":"python%e5%a6%82%e4%bd%95%e8%ae%a1%e7%ae%97%e4%b8%80%e7%bb%84ascii%e7%a0%81%e7%9a%84%e5%92%8c","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1135339.html","title":{"rendered":"python\u5982\u4f55\u8ba1\u7b97\u4e00\u7ec4ascii\u7801\u7684\u548c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104028\/faf252e4-d996-449c-b3ad-6287b3a3dae6.webp\" alt=\"python\u5982\u4f55\u8ba1\u7b97\u4e00\u7ec4ascii\u7801\u7684\u548c\" \/><\/p>\n<p><p> <strong>Python\u8ba1\u7b97\u4e00\u7ec4ASCII\u7801\u7684\u548c\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5229\u7528\u5185\u7f6e\u51fd\u6570ord()\u5c06\u5176\u8f6c\u6362\u4e3aASCII\u7801\uff0c\u5e76\u6c42\u548c<\/strong>\u3002<strong>ord()\u51fd\u6570<\/strong>\u662fPython\u5185\u7f6e\u51fd\u6570\uff0c\u7528\u4e8e\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u7684Unicode\u7801\u70b9\u3002\u672c\u6587\u5c06\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\uff0c\u5e76\u63d0\u4f9b\u8be6\u7ec6\u7684\u4ee3\u7801\u793a\u4f8b\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001Python\u4e2d\u7684ASCII\u7801\u7b80\u4ecb<\/h3>\n<\/p>\n<p><p>ASCII\uff08American Standard Code for Information Interchange\uff09\u662f\u4e00\u79cd\u57fa\u4e8e\u62c9\u4e01\u5b57\u6bcd\u7684\u5b57\u7b26\u7f16\u7801\u65b9\u6848\uff0c\u4e3b\u8981\u7528\u4e8e\u8868\u793a\u6587\u672c\u6570\u636e\u3002\u5728Python\u4e2d\uff0c\u6bcf\u4e2a\u5b57\u7b26\u90fd\u6709\u5bf9\u5e94\u7684ASCII\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528<code>ord()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u5b57\u7b26\u7684ASCII\u7801\uff0c\u5e76\u4f7f\u7528<code>chr()<\/code>\u51fd\u6570\u5c06ASCII\u7801\u8f6c\u6362\u4e3a\u5b57\u7b26\u3002\u4e86\u89e3\u8fd9\u4e9b\u57fa\u7840\u77e5\u8bc6\u6709\u52a9\u4e8e\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u548c\u5b9e\u73b0\u8ba1\u7b97ASCII\u7801\u4e4b\u548c\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001ord()\u548cchr()\u51fd\u6570\u8be6\u89e3<\/h3>\n<\/p>\n<p><h4>1\u3001ord()\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>ord()<\/code>\u51fd\u6570\u7528\u4e8e\u8fd4\u56de\u5355\u4e2a\u5b57\u7b26\u7684Unicode\u7801\u70b9\uff08\u5373ASCII\u7801\uff09\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(ord(&#39;A&#39;))  # \u8f93\u51fa 65<\/p>\n<p>print(ord(&#39;a&#39;))  # \u8f93\u51fa 97<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001chr()\u51fd\u6570<\/h4>\n<\/p>\n<p><p><code>chr()<\/code>\u51fd\u6570\u5219\u7528\u4e8e\u5c06\u6574\u6570\u8f6c\u6362\u4e3a\u5bf9\u5e94\u7684\u5b57\u7b26\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">print(chr(65))  # \u8f93\u51fa &#39;A&#39;<\/p>\n<p>print(chr(97))  # \u8f93\u51fa &#39;a&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u8ba1\u7b97ASCII\u7801\u4e4b\u548c\u7684\u5177\u4f53\u6b65\u9aa4<\/h3>\n<\/p>\n<p><h4>1\u3001\u904d\u5386\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u3002\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u83b7\u53d6\u6bcf\u4e2a\u5b57\u7b26\u7684ASCII\u7801<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>ord()<\/code>\u51fd\u6570\u83b7\u53d6\u6bcf\u4e2a\u5b57\u7b26\u7684ASCII\u7801\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u6c42\u548c<\/h4>\n<\/p>\n<p><p>\u5c06\u6240\u6709ASCII\u7801\u7d2f\u52a0\u5f97\u5230\u603b\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5b8c\u6574\u7684Python\u4ee3\u7801\u793a\u4f8b\uff0c\u7528\u4e8e\u8ba1\u7b97\u4e00\u7ec4ASCII\u7801\u7684\u548c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_ascii_sum(input_string):<\/p>\n<p>    total_sum = 0<\/p>\n<p>    for char in input_string:<\/p>\n<p>        total_sum += ord(char)<\/p>\n<p>    return total_sum<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>input_string = &quot;Hello, World!&quot;<\/p>\n<p>ascii_sum = calculate_ascii_sum(input_string)<\/p>\n<p>print(f&quot;\u8f93\u5165\u5b57\u7b26\u4e32\u7684ASCII\u7801\u4e4b\u548c\u4e3a: {ascii_sum}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4ee3\u7801\u89e3\u6790<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b9a\u4e49\u51fd\u6570<\/h4>\n<\/p>\n<p><p>\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u540d\u4e3a<code>calculate_ascii_sum<\/code>\u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u5b57\u7b26\u4e32\u53c2\u6570<code>input_string<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u521d\u59cb\u5316\u603b\u548c\u53d8\u91cf<\/h4>\n<\/p>\n<p><p>\u5728\u51fd\u6570\u5185\u90e8\uff0c\u6211\u4eec\u521d\u59cb\u5316\u4e00\u4e2a\u53d8\u91cf<code>total_sum<\/code>\u4e3a0\uff0c\u7528\u4e8e\u7d2f\u52a0\u6bcf\u4e2a\u5b57\u7b26\u7684ASCII\u7801\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u904d\u5386\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528for\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5e76\u4f7f\u7528<code>ord()<\/code>\u51fd\u6570\u83b7\u53d6\u5176ASCII\u7801\uff0c\u7136\u540e\u7d2f\u52a0\u5230<code>total_sum<\/code>\u3002<\/p>\n<\/p>\n<p><h4>4\u3001\u8fd4\u56de\u603b\u548c<\/h4>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u51fd\u6570\u8fd4\u56de\u7d2f\u52a0\u7684\u603b\u548c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><p>\u8ba1\u7b97ASCII\u7801\u4e4b\u548c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u6709\u5f88\u591a\u573a\u666f\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><h4>1\u3001\u7b80\u5355\u52a0\u5bc6\u89e3\u5bc6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7\u8ba1\u7b97\u5b57\u7b26\u4e32\u7684ASCII\u7801\u4e4b\u548c\u6765\u8fdb\u884c\u7b80\u5355\u7684\u52a0\u5bc6\u548c\u89e3\u5bc6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u6570\u636e\u6821\u9a8c<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u4f20\u8f93\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528ASCII\u7801\u4e4b\u548c\u8fdb\u884c\u6821\u9a8c\uff0c\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\u3002<\/p>\n<\/p>\n<p><h4>3\u3001\u5b57\u7b26\u7f16\u7801\u8f6c\u6362<\/h4>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u8fdb\u884c\u5b57\u7b26\u7f16\u7801\u8f6c\u6362\uff0c\u53ef\u4ee5\u901a\u8fc7ASCII\u7801\u8fdb\u884c\u4e2d\u95f4\u5904\u7406\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f18\u5316\u4e0e\u6269\u5c55<\/h3>\n<\/p>\n<p><h4>1\u3001\u5904\u7406\u7a7a\u5b57\u7b26\u4e32<\/h4>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u5904\u7406\u7a7a\u5b57\u7b26\u4e32\u7684\u60c5\u51b5\u3002\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u6dfb\u52a0\u5224\u65ad\uff0c\u82e5\u8f93\u5165\u4e3a\u7a7a\u5b57\u7b26\u4e32\uff0c\u5219\u8fd4\u56de0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_ascii_sum(input_string):<\/p>\n<p>    if not input_string:<\/p>\n<p>        return 0<\/p>\n<p>    total_sum = 0<\/p>\n<p>    for char in input_string:<\/p>\n<p>        total_sum += ord(char)<\/p>\n<p>    return total_sum<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u652f\u6301\u591a\u79cd\u5b57\u7b26\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u6269\u5c55\u51fd\u6570\uff0c\u652f\u6301\u591a\u79cd\u5b57\u7b26\u7f16\u7801\uff0c\u4f8b\u5982UTF-8\u3001UTF-16\u7b49\u3002\u4f7f\u7528Python\u7684<code>encode()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u6570\u7ec4\uff0c\u7136\u540e\u8ba1\u7b97\u6bcf\u4e2a\u5b57\u8282\u7684\u548c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def calculate_ascii_sum(input_string, encoding=&#39;utf-8&#39;):<\/p>\n<p>    if not input_string:<\/p>\n<p>        return 0<\/p>\n<p>    byte_array = input_string.encode(encoding)<\/p>\n<p>    total_sum = sum(byte_array)<\/p>\n<p>    return total_sum<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u63a2\u8ba8\u4e86\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u4e00\u7ec4ASCII\u7801\u7684\u548c\u3002\u6211\u4eec\u4e0d\u4ec5\u4ecb\u7ecd\u4e86\u57fa\u672c\u7684<code>ord()<\/code>\u548c<code>chr()<\/code>\u51fd\u6570\uff0c\u8fd8\u63d0\u4f9b\u4e86\u8be6\u7ec6\u7684\u4ee3\u7801\u793a\u4f8b\u548c\u5e94\u7528\u573a\u666f\u3002\u6b64\u5916\uff0c\u6211\u4eec\u8fd8\u8ba8\u8bba\u4e86\u4f18\u5316\u548c\u6269\u5c55\u7684\u53ef\u80fd\u6027\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528Python\u4e2d\u7684\u5b57\u7b26\u7f16\u7801\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u5b57\u7b26\u4e32\u7684ASCII\u7801\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>ord()<\/code>\u51fd\u6570\u6765\u83b7\u53d6\u5b57\u7b26\u4e32\u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684ASCII\u7801\u503c\u3002\u53ea\u9700\u904d\u5386\u5b57\u7b26\u4e32\uff0c\u5c06\u6bcf\u4e2a\u5b57\u7b26\u7684ASCII\u503c\u5b58\u50a8\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u6700\u540e\u4f7f\u7528<code>sum()<\/code>\u51fd\u6570\u5bf9\u8fd9\u4e9b\u503c\u8fdb\u884c\u6c42\u548c\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">string = &quot;hello&quot;\nascii_values = [ord(char) for char in string]\nascii_sum = sum(ascii_values)\nprint(ascii_sum)  # \u8f93\u51faASCII\u7801\u7684\u548c\n<\/code><\/pre>\n<p><strong>Python\u4e2d\u5982\u4f55\u5904\u7406\u975eASCII\u5b57\u7b26\u7684\u60c5\u51b5\uff1f<\/strong><br \/>\u5904\u7406\u5305\u542b\u975eASCII\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u4f7f\u7528<code>ord()<\/code>\u51fd\u6570\u540c\u6837\u9002\u7528\uff0c\u4f46\u8981\u6ce8\u610f\uff0c\u975eASCII\u5b57\u7b26\u7684ASCII\u503c\u5c06\u8d85\u51fa\u5e38\u89c4\u8303\u56f4\uff080-127\uff09\u3002\u4e3a\u4e86\u53ea\u8ba1\u7b97ASCII\u5b57\u7b26\u7684\u548c\uff0c\u53ef\u4ee5\u5728\u8ba1\u7b97\u524d\u5148\u8fc7\u6ee4\u5b57\u7b26\u4e32\uff0c\u53ea\u4fdd\u7559ASCII\u5b57\u7b26\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">string = &quot;hello, \u4e16\u754c&quot;\nascii_sum = sum(ord(char) for char in string if ord(char) &lt; 128)\nprint(ascii_sum)  # \u53ea\u8ba1\u7b97ASCII\u5b57\u7b26\u7684\u548c\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u4f18\u5316\u8ba1\u7b97ASCII\u7801\u548c\u7684\u6548\u7387\uff1f<\/strong><br \/>\u5728\u5904\u7406\u8f83\u957f\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u91c7\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u6765\u51cf\u5c11\u5185\u5b58\u5360\u7528\uff0c\u8fd9\u6837\u5728\u8ba1\u7b97ASCII\u7801\u548c\u65f6\uff0c\u7a0b\u5e8f\u4f1a\u66f4\u52a0\u9ad8\u6548\u3002\u4f7f\u7528\u751f\u6210\u5668\u8868\u8fbe\u5f0f\u53ef\u4ee5\u907f\u514d\u521b\u5efa\u4e2d\u95f4\u5217\u8868\uff0c\u4ece\u800c\u8282\u7701\u5185\u5b58\uff1a<\/p>\n<pre><code class=\"language-python\">string = &quot;example&quot;\nascii_sum = sum(ord(char) for char in string)\nprint(ascii_sum)  # \u8ba1\u7b97ASCII\u7801\u7684\u548c\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u8ba1\u7b97\u8fc7\u7a0b\u5c06\u66f4\u52a0\u9ad8\u6548\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u65f6\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8ba1\u7b97\u4e00\u7ec4ASCII\u7801\u7684\u548c\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u5229\u7528\u5185\u7f6e\u51fd\u6570ord()\u5c06\u5176\u8f6c\u6362\u4e3aASCII [&hellip;]","protected":false},"author":3,"featured_media":1135345,"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\/1135339"}],"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=1135339"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135339\/revisions"}],"predecessor-version":[{"id":1135347,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135339\/revisions\/1135347"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1135345"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1135339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1135339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1135339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}