{"id":1106679,"date":"2025-01-08T16:42:26","date_gmt":"2025-01-08T08:42:26","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1106679.html"},"modified":"2025-01-08T16:42:28","modified_gmt":"2025-01-08T08:42:28","slug":"python%e5%a6%82%e4%bd%95%e5%88%a4%e6%96%ad%e4%b8%ad%e6%96%87%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2%e9%95%bf%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1106679.html","title":{"rendered":"python\u5982\u4f55\u5224\u65ad\u4e2d\u6587\u7684\u5b57\u7b26\u4e32\u957f\u5ea6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071034\/fe6f6ac8-c15c-4b87-ac6e-cbaa43672ac5.webp\" alt=\"python\u5982\u4f55\u5224\u65ad\u4e2d\u6587\u7684\u5b57\u7b26\u4e32\u957f\u5ea6\" \/><\/p>\n<p><p> <strong>\u8981\u5224\u65ad\u4e2d\u6587\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u53ef\u4ee5\u4f7f\u7528len()\u51fd\u6570\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5e93\u3001\u4f7f\u7528unicodedata\u5e93\u3001\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528len()\u51fd\u6570\u662f\u6700\u5e38\u89c1\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u63cf\u8ff0\u4f7f\u7528len()\u51fd\u6570\u5224\u65ad\u4e2d\u6587\u5b57\u7b26\u4e32\u957f\u5ea6\u7684\u5177\u4f53\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528len()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0clen()\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u7528\u4e8e\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u65e0\u8bba\u5b57\u7b26\u4e32\u4e2d\u5305\u542b\u7684\u662f\u4e2d\u6587\u5b57\u7b26\u8fd8\u662f\u5176\u4ed6\u5b57\u7b26\u3002\u6bcf\u4e00\u4e2a\u4e2d\u6587\u5b57\u7b26\u5728Python\u4e2d\u90fd\u88ab\u89c6\u4e3a\u4e00\u4e2a\u5b57\u7b26\uff0c\u56e0\u6b64len()\u51fd\u6570\u8ba1\u7b97\u7684\u5c31\u662f\u5b57\u7b26\u7684\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">chinese_string = &quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;<\/p>\n<p>length = len(chinese_string)<\/p>\n<p>print(f&quot;\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u662f\uff1a{length}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u5b57\u7b26\u4e32 <code>&quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;<\/code> \u5305\u542b5\u4e2a\u5b57\u7b26\uff0c\u56e0\u6b64 <code>len()<\/code> \u51fd\u6570\u4f1a\u8fd4\u56de 5\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u662f\u6700\u76f4\u63a5\u4e5f\u662f\u6700\u5e38\u7528\u7684\u5224\u65ad\u5b57\u7b26\u4e32\u957f\u5ea6\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u4efb\u4f55\u5305\u542b\u4e2d\u6587\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u5728\u5904\u7406\u5b57\u7b26\u4e32\u65f6\u975e\u5e38\u5f3a\u5927\uff0c\u4f7f\u7528\u5b83\u53ef\u4ee5\u66f4\u52a0\u7075\u6d3b\u5730\u83b7\u53d6\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u8fc7\u6ee4\u6216\u5339\u914d\u7279\u5b9a\u7c7b\u578b\u5b57\u7b26\u65f6\u3002Python\u7684 <code>re<\/code> \u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0\u8fd9\u4e00\u70b9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>chinese_string = &quot;\u4f60\u597d\uff0c\u4e16\u754c123&quot;<\/p>\n<h2><strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6240\u6709\u6c49\u5b57<\/strong><\/h2>\n<p>pattern = re.compile(r&#39;[\\u4e00-\\u9fff]+&#39;)<\/p>\n<p>matches = pattern.findall(chinese_string)<\/p>\n<p>length = sum(len(match) for match in matches)<\/p>\n<p>print(f&quot;\u4e2d\u6587\u5b57\u7b26\u7684\u603b\u957f\u5ea6\u662f\uff1a{length}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>re.compile()<\/code> \u7f16\u8bd1\u4e86\u4e00\u4e2a\u5339\u914d\u6240\u6709\u4e2d\u6587\u5b57\u7b26\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u7136\u540e\u4f7f\u7528 <code>findall()<\/code> \u65b9\u6cd5\u627e\u5230\u6240\u6709\u7684\u5339\u914d\u9879\u3002\u6700\u540e\uff0c\u901a\u8fc7\u8ba1\u7b97\u6240\u6709\u5339\u914d\u9879\u7684\u957f\u5ea6\u6765\u5f97\u51fa\u4e2d\u6587\u5b57\u7b26\u7684\u603b\u957f\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528unicodedata\u5e93<\/h3>\n<\/p>\n<p><p><code>unicodedata<\/code> \u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u7ec6\u81f4\u5730\u5904\u7406Unicode\u5b57\u7b26\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u533a\u5206\u4e0d\u540c\u7c7b\u578b\u7684Unicode\u5b57\u7b26\u65f6\u3002\u8fd9\u4e2a\u5e93\u5bf9\u4e8e\u5904\u7406\u590d\u6742\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\uff08\u5982\u5224\u65ad\u5b57\u7b26\u7c7b\u522b\uff09\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import unicodedata<\/p>\n<p>chinese_string = &quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;<\/p>\n<p>length = 0<\/p>\n<p>for char in chinese_string:<\/p>\n<p>    if unicodedata.category(char).startswith(&#39;Lo&#39;):<\/p>\n<p>        length += 1<\/p>\n<p>print(f&quot;\u4e2d\u6587\u5b57\u7b26\u7684\u957f\u5ea6\u662f\uff1a{length}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e00\u4e2a\u5b57\u7b26\uff0c\u5e76\u4f7f\u7528 <code>unicodedata.category()<\/code> \u65b9\u6cd5\u5224\u65ad\u5b83\u662f\u5426\u662f\u4e00\u4e2a\u4e2d\u6587\u5b57\u7b26\uff08\u5373\u7c7b\u522b\u4e3a &quot;Lo&quot; \u7684\u5b57\u7b26\uff09\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u7a0d\u5fae\u590d\u6742\uff0c\u4f46\u5728\u9700\u8981\u5904\u7406\u4e0d\u540c\u7c7b\u578b\u7684Unicode\u5b57\u7b26\u65f6\u975e\u5e38\u6709\u7528\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32<\/h3>\n<\/p>\n<p><p>\u5c3d\u7ba1\u8fd9\u79cd\u65b9\u6cd5\u6bd4\u8f83\u4f4e\u6548\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u60c5\u51b5\u4e0b\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\u4e5f\u662f\u4e00\u79cd\u9009\u62e9\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u624b\u52a8\u68c0\u67e5\u6bcf\u4e00\u4e2a\u5b57\u7b26\u662f\u5426\u662f\u4e2d\u6587\u5b57\u7b26\uff0c\u5e76\u8ba1\u7b97\u5176\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">chinese_string = &quot;\u4f60\u597d\uff0c\u4e16\u754c&quot;<\/p>\n<p>length = 0<\/p>\n<p>for char in chinese_string:<\/p>\n<p>    if &#39;\\u4e00&#39; &lt;= char &lt;= &#39;\\u9fff&#39;:<\/p>\n<p>        length += 1<\/p>\n<p>print(f&quot;\u4e2d\u6587\u5b57\u7b26\u7684\u957f\u5ea6\u662f\uff1a{length}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u76f4\u63a5\u68c0\u67e5\u6bcf\u4e2a\u5b57\u7b26\u662f\u5426\u5728\u4e2d\u6587\u5b57\u7b26\u7684Unicode\u8303\u56f4\u5185\uff08\u5373\u4ece <code>\\u4e00<\/code> \u5230 <code>\\u9fff<\/code>\uff09\u3002<strong>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u6613\u61c2\uff0c\u4f46\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u53ef\u80fd\u4e0d\u5982\u524d\u9762\u4ecb\u7ecd\u7684\u65b9\u6cd5\u9ad8\u6548\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u5c0f\u7ed3<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u51e0\u79cd\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u5224\u65ad\u4e00\u4e2a\u5305\u542b\u4e2d\u6587\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u3002<strong>\u4f7f\u7528 <code>len()<\/code> \u51fd\u6570\u662f\u6700\u7b80\u5355\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5<\/strong>\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u60c5\u51b5\uff1b<strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u548c <code>unicodedata<\/code> \u5e93\u5219\u63d0\u4f9b\u4e86\u66f4\u7075\u6d3b\u7684\u5904\u7406\u65b9\u5f0f<\/strong>\uff0c\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u9700\u6c42\uff1b\u800c<strong>\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\u7684\u65b9\u6cd5<\/strong>\u5c3d\u7ba1\u4e0d\u5982\u524d\u51e0\u79cd\u65b9\u6cd5\u9ad8\u6548\uff0c\u4f46\u5728\u67d0\u4e9b\u7279\u5b9a\u60c5\u51b5\u4e0b\u4e5f\u53ef\u4ee5\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u4e00\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u6839\u636e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u6765\u51b3\u5b9a\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u7406\u89e3\u5982\u4f55\u5224\u65ad\u4e2d\u6587\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u8ba1\u7b97\u5b57\u7b26\u4e32\u4e2d\u4e2d\u6587\u5b57\u7b26\u7684\u6570\u91cf\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>len()<\/code>\u51fd\u6570\u6765\u8ba1\u7b97\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u4f46\u8981\u7279\u522b\u6ce8\u610f\u4e2d\u6587\u5b57\u7b26\u7684\u5904\u7406\u3002\u7531\u4e8e\u4e2d\u6587\u5b57\u7b26\u5728\u5185\u5b58\u4e2d\u5360\u7528\u591a\u4e2a\u5b57\u8282\uff0c\u76f4\u63a5\u4f7f\u7528<code>len()<\/code>\u53ef\u80fd\u4f1a\u5bfc\u81f4\u7ed3\u679c\u4e0d\u51c6\u786e\u3002\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u5e76\u5224\u65ad\u6bcf\u4e2a\u5b57\u7b26\u662f\u5426\u4e3a\u4e2d\u6587\u6765\u51c6\u786e\u8ba1\u7b97\u4e2d\u6587\u5b57\u7b26\u7684\u6570\u91cf\u3002\u4f7f\u7528<code>str.isalpha()<\/code>\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u5e2e\u52a9\u8bc6\u522b\u4e2d\u6587\u5b57\u7b26\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u5305\u542b\u4e2d\u82f1\u6587\u6df7\u5408\u7684\u5b57\u7b26\u4e32\u957f\u5ea6\u8ba1\u7b97\uff1f<\/strong><br \/>\u5bf9\u4e8e\u5305\u542b\u4e2d\u82f1\u6587\u6df7\u5408\u7684\u5b57\u7b26\u4e32\uff0c\u8ba1\u7b97\u957f\u5ea6\u65f6\u8981\u8003\u8651\u5230\u4e0d\u540c\u5b57\u7b26\u7684\u5b57\u8282\u6570\u3002\u53ef\u4ee5\u4f7f\u7528<code>unicodedata<\/code>\u6a21\u5757\u4e2d\u7684<code>category<\/code>\u51fd\u6570\u6765\u5224\u65ad\u5b57\u7b26\u7c7b\u578b\uff0c\u6216\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5206\u522b\u7edf\u8ba1\u4e2d\u6587\u548c\u82f1\u6587\u5b57\u7b26\u7684\u6570\u91cf\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u7cbe\u786e\u7684\u957f\u5ea6\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5426\u5168\u4e3a\u4e2d\u6587\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5224\u65ad\u5b57\u7b26\u4e32\u662f\u5426\u5168\u4e3a\u4e2d\u6587\u3002\u4f8b\u5982\uff0c\u4f7f\u7528\u6a21\u5f0f<code>^[\\u4e00-\\u9fa5]+$<\/code>\u53ef\u4ee5\u5339\u914d\u6240\u6709\u4e2d\u6587\u5b57\u7b26\u3002\u5982\u679c\u5b57\u7b26\u4e32\u5b8c\u5168\u7b26\u5408\u8fd9\u4e2a\u6a21\u5f0f\uff0c\u90a3\u4e48\u53ef\u4ee5\u786e\u5b9a\u5b83\u662f\u5168\u4e2d\u6587\u5b57\u7b26\u4e32\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u5904\u7406\u7528\u6237\u8f93\u5165\u65f6\u975e\u5e38\u6709\u7528\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u8fdb\u884c\u9a8c\u8bc1\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5224\u65ad\u4e2d\u6587\u5b57\u7b26\u4e32\u7684\u957f\u5ea6\uff0c\u53ef\u4ee5\u4f7f\u7528len()\u51fd\u6570\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5e93\u3001\u4f7f\u7528unicodedata\u5e93\u3001\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32 [&hellip;]","protected":false},"author":3,"featured_media":1106689,"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\/1106679"}],"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=1106679"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1106679\/revisions"}],"predecessor-version":[{"id":1106690,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1106679\/revisions\/1106690"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1106689"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1106679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1106679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1106679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}