{"id":994618,"date":"2024-12-27T08:57:01","date_gmt":"2024-12-27T00:57:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/994618.html"},"modified":"2024-12-27T08:57:07","modified_gmt":"2024-12-27T00:57:07","slug":"python%e5%a6%82%e4%bd%95%e6%a3%80%e6%9f%a5%e4%b8%ad%e6%96%87%e6%8b%ac%e5%8f%b7","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/994618.html","title":{"rendered":"python\u5982\u4f55\u68c0\u67e5\u4e2d\u6587\u62ec\u53f7"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25071523\/48e57e35-318d-403a-9f8b-661ff79f4b6c.webp\" alt=\"python\u5982\u4f55\u68c0\u67e5\u4e2d\u6587\u62ec\u53f7\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u68c0\u67e5\u4e2d\u6587\u62ec\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u904d\u5386\u5b57\u7b26\u4e32\u3001\u5229\u7528\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b\u7684\u5de5\u5177\uff0c\u7279\u522b\u9002\u5408\u5904\u7406\u8fd9\u79cd\u6a21\u5f0f\u5339\u914d\u7684\u95ee\u9898\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6b63\u5219\u8868\u8fbe\u5f0f\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>re<\/code>\u6a21\u5757\u6765\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u4e2d\u6587\u62ec\u53f7\u3002\u5728\u8fd9\u4e2a\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u5339\u914d\u6a21\u5f0f\uff0c\u4e13\u95e8\u7528\u4e8e\u68c0\u6d4b\u4e2d\u6587\u62ec\u53f7\u3002<\/p>\n<\/p>\n<p><p>\u5728\u8be6\u7ec6\u63cf\u8ff0\u6b63\u5219\u8868\u8fbe\u5f0f\u65b9\u6cd5\u4e4b\u524d\uff0c\u8ba9\u6211\u4eec\u63a2\u8ba8\u4e00\u4e0b\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u4e2d\u6587\u62ec\u53f7\u7684\u4e0d\u540c\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/p>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u5339\u914d\u590d\u6742\u7684\u6a21\u5f0f\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>re<\/code>\u6a21\u5757\u6765\u5904\u7406\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u5bf9\u4e8e\u68c0\u67e5\u4e2d\u6587\u62ec\u53f7\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165<code>re<\/code>\u6a21\u5757\u3002<\/li>\n<li>\u5b9a\u4e49\u4e00\u4e2a\u6b63\u5219\u8868\u8fbe\u5f0f\u6a21\u5f0f\u6765\u5339\u914d\u4e2d\u6587\u62ec\u53f7\u3002\u4e2d\u6587\u62ec\u53f7\u7684Unicode\u7f16\u7801\u4e3a<code>\uff08<\/code>\u548c<code>\uff09<\/code>\uff0c\u56e0\u6b64\u5339\u914d\u6a21\u5f0f\u4e3a<code>[\\uFF08\\uFF09]<\/code>\u3002<\/li>\n<li>\u4f7f\u7528<code>re.findall<\/code>\u65b9\u6cd5\u5728\u5b57\u7b26\u4e32\u4e2d\u67e5\u627e\u6240\u6709\u5339\u914d\u9879\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>def find_chinese_parentheses(text):<\/p>\n<p>    pattern = r&#39;[\\uFF08\\uFF09]&#39;<\/p>\n<p>    matches = re.findall(pattern, text)<\/p>\n<p>    return matches<\/p>\n<p>text = &quot;\u8fd9\u662f\u4e00\u4e2a\u5305\u542b\u4e2d\u6587\u62ec\u53f7\u7684\u793a\u4f8b\uff08\u6d4b\u8bd5\uff09&quot;<\/p>\n<p>print(find_chinese_parentheses(text))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6a21\u5f0f<code>[\\uFF08\\uFF09]<\/code>\u7528\u6765\u5339\u914d\u5b57\u7b26\u4e32\u4e2d\u7684\u4e2d\u6587\u5de6\u62ec\u53f7<code>\uff08<\/code>\u548c\u53f3\u62ec\u53f7<code>\uff09<\/code>\u3002<code>re.findall<\/code>\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5339\u914d\u9879\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u904d\u5386\u5b57\u7b26\u4e32<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5916\uff0c\u8fd8\u53ef\u4ee5\u901a\u8fc7\u904d\u5386\u5b57\u7b26\u4e32\u6765\u68c0\u67e5\u4e2d\u6587\u62ec\u53f7\u3002\u8fd9\u79cd\u65b9\u6cd5\u8f83\u4e3a\u76f4\u89c2\uff0c\u4f46\u4e0d\u5982\u6b63\u5219\u8868\u8fbe\u5f0f\u7b80\u6d01\u3002\u4ee5\u4e0b\u662f\u5b9e\u73b0\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u521d\u59cb\u5316\u4e00\u4e2a\u5217\u8868\u7528\u4e8e\u5b58\u50a8\u627e\u5230\u7684\u62ec\u53f7\u3002<\/li>\n<li>\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\u3002<\/li>\n<li>\u68c0\u67e5\u5b57\u7b26\u662f\u5426\u4e3a\u4e2d\u6587\u62ec\u53f7\u3002<\/li>\n<li>\u5982\u679c\u662f\uff0c\u5c06\u5176\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">def find_chinese_parentheses(text):<\/p>\n<p>    chinese_parentheses = [&#39;\uff08&#39;, &#39;\uff09&#39;]<\/p>\n<p>    found_parentheses = []<\/p>\n<p>    for char in text:<\/p>\n<p>        if char in chinese_parentheses:<\/p>\n<p>            found_parentheses.append(char)<\/p>\n<p>    return found_parentheses<\/p>\n<p>text = &quot;\u8fd9\u662f\u4e00\u4e2a\u5305\u542b\u4e2d\u6587\u62ec\u53f7\u7684\u793a\u4f8b\uff08\u6d4b\u8bd5\uff09&quot;<\/p>\n<p>print(find_chinese_parentheses(text))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u867d\u7136\u76f4\u89c2\uff0c\u4f46\u5728\u6027\u80fd\u4e0a\u53ef\u80fd\u4e0d\u5982\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u56e0\u4e3a\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5728\u5e95\u5c42\u8fdb\u884c\u4f18\u5316\u7684\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u5229\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>Python\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u4e5f\u53ef\u4ee5\u7528\u4e8e\u68c0\u67e5\u4e2d\u6587\u62ec\u53f7\u3002\u867d\u7136\u6ca1\u6709\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u4f46\u53ef\u4ee5\u7ed3\u5408<code>str.find<\/code>\u6216<code>str.index<\/code>\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528<code>str.find<\/code>\u65b9\u6cd5\u67e5\u627e\u62ec\u53f7\u7684\u4f4d\u7f6e\u3002<\/li>\n<li>\u5982\u679c\u627e\u5230\uff0c\u8fd4\u56de\u4f4d\u7f6e\u6216\u8bb0\u5f55\u62ec\u53f7\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">def find_chinese_parentheses(text):<\/p>\n<p>    positions = []<\/p>\n<p>    start = 0<\/p>\n<p>    while True:<\/p>\n<p>        pos = text.find(&#39;\uff08&#39;, start)<\/p>\n<p>        if pos == -1:<\/p>\n<p>            break<\/p>\n<p>        positions.append((&#39;\uff08&#39;, pos))<\/p>\n<p>        start = pos + 1<\/p>\n<p>    start = 0<\/p>\n<p>    while True:<\/p>\n<p>        pos = text.find(&#39;\uff09&#39;, start)<\/p>\n<p>        if pos == -1:<\/p>\n<p>            break<\/p>\n<p>        positions.append((&#39;\uff09&#39;, pos))<\/p>\n<p>        start = pos + 1<\/p>\n<p>    return positions<\/p>\n<p>text = &quot;\u8fd9\u662f\u4e00\u4e2a\u5305\u542b\u4e2d\u6587\u62ec\u53f7\u7684\u793a\u4f8b\uff08\u6d4b\u8bd5\uff09&quot;<\/p>\n<p>print(find_chinese_parentheses(text))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7684\u597d\u5904\u662f\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56de\u62ec\u53f7\u7684\u4f4d\u7f6e\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3\u4e0e\u6269\u5c55\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u68c0\u67e5\u4e2d\u6587\u62ec\u53f7\u5728\u6587\u672c\u5904\u7406\u4e2d\u53ef\u80fd\u662f\u4e00\u4e2a\u5e38\u89c1\u7684\u9700\u6c42\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u4e2d\u6587\u6587\u672c\u65f6\u3002\u65e0\u8bba\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u5b57\u7b26\u4e32\u904d\u5386\u8fd8\u662f\u5b57\u7b26\u4e32\u65b9\u6cd5\uff0c\u90fd\u5404\u6709\u5176\u4f18\u7f3a\u70b9\u3002<\/p>\n<\/p>\n<p><p><strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u65b9\u6cd5<\/strong>\uff1a\u975e\u5e38\u5f3a\u5927\u4e14\u7b80\u6d01\uff0c\u9002\u7528\u4e8e\u590d\u6742\u6a21\u5f0f\u5339\u914d\u7684\u573a\u666f\u3002\u5728\u5904\u7406\u5927\u91cf\u6587\u672c\u65f6\uff0c\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6027\u80fd\u53ef\u80fd\u4f18\u4e8e\u5176\u4ed6\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5176\u5e95\u5c42\u7ecf\u8fc7\u4f18\u5316\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b57\u7b26\u4e32\u904d\u5386\u65b9\u6cd5<\/strong>\uff1a\u7b80\u5355\u76f4\u89c2\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u5b9e\u73b0\uff0c\u4f46\u5728\u5904\u7406\u5927\u89c4\u6a21\u6587\u672c\u65f6\u53ef\u80fd\u4e0d\u5982\u6b63\u5219\u8868\u8fbe\u5f0f\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><p><strong>\u5b57\u7b26\u4e32\u65b9\u6cd5<\/strong>\uff1a\u53ef\u4ee5\u76f4\u63a5\u83b7\u53d6\u62ec\u53f7\u7684\u4f4d\u7f6e\uff0c\u9002\u5408\u9700\u8981\u5bf9\u62ec\u53f7\u8fdb\u884c\u8fdb\u4e00\u6b65\u64cd\u4f5c\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u9700\u6c42\u548c\u4e0a\u4e0b\u6587\u3002\u5982\u679c\u9700\u8981\u5904\u7406\u5927\u91cf\u6587\u672c\u4e14\u5bf9\u6027\u80fd\u8981\u6c42\u8f83\u9ad8\uff0c\u5efa\u8bae\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u5982\u679c\u53ea\u662f\u7b80\u5355\u5730\u68c0\u67e5\u5c0f\u89c4\u6a21\u6587\u672c\uff0c\u5b57\u7b26\u4e32\u904d\u5386\u65b9\u6cd5\u4e5f\u5b8c\u5168\u80dc\u4efb\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u4e0d\u4ec5\u9650\u4e8e\u68c0\u67e5\u4e2d\u6587\u62ec\u53f7\uff0c\u8fd8\u53ef\u4ee5\u6269\u5c55\u5230\u5176\u4ed6\u5b57\u7b26\u7684\u5339\u914d\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7528\u6765\u68c0\u67e5\u5176\u4ed6\u7279\u5b9a\u7684\u6807\u70b9\u7b26\u53f7\u3001\u5b57\u7b26\u6216\u6587\u672c\u6a21\u5f0f\u3002\u901a\u8fc7\u9002\u5f53\u4fee\u6539\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u5b57\u7b26\u5217\u8868\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u5bf9\u5176\u4ed6\u5b57\u7b26\u7684\u68c0\u67e5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bc6\u522b\u548c\u66ff\u6362\u4e2d\u6587\u62ec\u53f7\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u8bc6\u522b\u4e2d\u6587\u62ec\u53f7\u3002\u901a\u8fc7<code>re<\/code>\u6a21\u5757\uff0c\u53ef\u4ee5\u5b9a\u4e49\u5339\u914d\u4e2d\u6587\u62ec\u53f7\u7684\u6a21\u5f0f\uff0c\u5e76\u4f7f\u7528<code>re.sub()<\/code>\u51fd\u6570\u6765\u66ff\u6362\u5b83\u4eec\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>re.findall()<\/code>\u53ef\u4ee5\u627e\u51fa\u6240\u6709\u7684\u4e2d\u6587\u62ec\u53f7\u5e76\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u65f6\uff0c\u5982\u4f55\u5224\u65ad\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5305\u542b\u4e2d\u6587\u62ec\u53f7\uff1f<\/strong><br \/>\u53ef\u4ee5\u5229\u7528\u5b57\u7b26\u4e32\u7684<code>in<\/code>\u5173\u952e\u5b57\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5224\u65ad\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u542b\u6709\u4e2d\u6587\u62ec\u53f7\u3002\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u6a21\u5f0f\uff0c\u68c0\u67e5\u5b57\u7b26\u4e32\u4e2d\u662f\u5426\u5b58\u5728<code>\uff08<\/code>\u6216<code>\uff09<\/code>\u3002\u5982\u679c\u5b58\u5728\uff0c\u4fbf\u8bf4\u660e\u8be5\u5b57\u7b26\u4e32\u5305\u542b\u4e2d\u6587\u62ec\u53f7\u3002<\/p>\n<p><strong>\u6709\u6ca1\u6709\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u7edf\u8ba1\u6587\u672c\u4e2d\u4e2d\u6587\u62ec\u53f7\u7684\u6570\u91cf\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u904d\u5386\u5b57\u7b26\u4e32\u4e2d\u7684\u6bcf\u4e2a\u5b57\u7b26\uff0c\u6216\u8005\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u7edf\u8ba1\u4e2d\u6587\u62ec\u53f7\u7684\u6570\u91cf\u3002\u4f7f\u7528<code>str.count()<\/code>\u65b9\u6cd5\u4e5f\u80fd\u5feb\u901f\u5f97\u5230<code>\uff08<\/code>\u548c<code>\uff09<\/code>\u7684\u51fa\u73b0\u6b21\u6570\uff0c\u6700\u540e\u5c06\u4e24\u8005\u76f8\u52a0\u5373\u53ef\u83b7\u5f97\u603b\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u68c0\u67e5\u4e2d\u6587\u62ec\u53f7\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u904d\u5386\u5b57\u7b26\u4e32\u3001\u5229\u7528\u5185\u7f6e\u7684\u5b57\u7b26\u4e32\u65b9\u6cd5\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u4e00\u79cd\u5f3a\u5927\u4e14\u7075\u6d3b [&hellip;]","protected":false},"author":3,"featured_media":994630,"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\/994618"}],"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=994618"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994618\/revisions"}],"predecessor-version":[{"id":994638,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/994618\/revisions\/994638"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/994630"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=994618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=994618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=994618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}