{"id":1005451,"date":"2024-12-27T10:35:54","date_gmt":"2024-12-27T02:35:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1005451.html"},"modified":"2024-12-27T10:35:56","modified_gmt":"2024-12-27T02:35:56","slug":"python%e5%a6%82%e4%bd%95%e6%88%aa%e5%8f%96%e6%96%87%e6%9c%ac%e5%88%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1005451.html","title":{"rendered":"python\u5982\u4f55\u622a\u53d6\u6587\u672c\u5217"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25082003\/655f8ad9-9040-4b47-ac85-8c952202fc31.webp\" alt=\"python\u5982\u4f55\u622a\u53d6\u6587\u672c\u5217\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u622a\u53d6\u6587\u672c\u5217\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u3001\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u8fd0\u7528Pandas\u5e93\u3001\u4f7f\u7528csv\u6a21\u5757\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528Pandas\u5e93\u662f\u5904\u7406\u6570\u636e\u6700\u4e3a\u65b9\u4fbf\u5feb\u6377\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002Pandas\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u64cd\u4f5c\u80fd\u529b\uff0c\u53ef\u4ee5\u8f7b\u677e\u5bf9\u6587\u672c\u5217\u8fdb\u884c\u622a\u53d6\u548c\u8f6c\u6362\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Pandas\u5e93\u6765\u622a\u53d6\u6587\u672c\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b57\u7b26\u4e32\u5207\u7247<\/h3>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u5207\u7247\u662fPython\u4e2d\u5904\u7406\u5b57\u7b26\u4e32\u7684\u57fa\u672c\u65b9\u6cd5\u4e4b\u4e00\u3002\u901a\u8fc7\u6307\u5b9a\u8d77\u59cb\u548c\u7ed3\u675f\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u622a\u53d6\u5b57\u7b26\u4e32\u7684\u4e00\u90e8\u5206\u3002\u5bf9\u4e8e\u6bcf\u4e00\u884c\u6587\u672c\uff0c\u53ef\u4ee5\u6309\u76f8\u540c\u7684\u65b9\u5f0f\u8fdb\u884c\u5207\u7247\u3002<\/p>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u57fa\u672c\u5207\u7247<\/h4>\n<\/p>\n<p><p>\u5b57\u7b26\u4e32\u5207\u7247\u7684\u57fa\u672c\u683c\u5f0f\u4e3a<code>str[start:end]<\/code>\uff0c\u5176\u4e2d<code>start<\/code>\u662f\u8d77\u59cb\u7d22\u5f15\uff0c<code>end<\/code>\u662f\u7ed3\u675f\u7d22\u5f15\uff08\u4e0d\u5305\u542b\uff09\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u622a\u53d6\u6587\u672c\u4e2d\u7684\u6307\u5b9a\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">text = &quot;Hello, World!&quot;<\/p>\n<h2><strong>\u622a\u53d6\u4ece\u7d22\u5f151\u5230\u7d22\u5f155\u7684\u5b57\u7b26<\/strong><\/h2>\n<p>sliced_text = text[1:5]<\/p>\n<p>print(sliced_text)  # \u8f93\u51fa: ello<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4ece\u6587\u672c\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5e76\u5207\u7247<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u6587\u672c\u6570\u636e\u5b58\u50a8\u5728\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u5148\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u7136\u540e\u5bf9\u6bcf\u4e00\u884c\u8fdb\u884c\u5207\u7247\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;data.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        # \u5047\u8bbe\u9700\u8981\u622a\u53d6\u6bcf\u884c\u7684\u7b2c2\u5230\u7b2c6\u4e2a\u5b57\u7b26<\/p>\n<p>        sliced_line = line[1:6]<\/p>\n<p>        print(sliced_line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6b63\u5219\u8868\u8fbe\u5f0f<\/h3>\n<\/p>\n<p><p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u5904\u7406\u6587\u672c\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u66f4\u590d\u6742\u7684\u622a\u53d6\u9700\u6c42\u3002Python\u7684<code>re<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u64cd\u4f5c\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u672c\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u53ef\u4ee5\u6839\u636e\u6a21\u5f0f\u5339\u914d\u6765\u622a\u53d6\u6587\u672c\u4e2d\u7684\u7279\u5b9a\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>text = &quot;The price is $100&quot;<\/p>\n<h2><strong>\u5339\u914d\u6570\u5b57\u90e8\u5206<\/strong><\/h2>\n<p>match = re.search(r&#39;\\d+&#39;, text)<\/p>\n<p>if match:<\/p>\n<p>    print(match.group())  # \u8f93\u51fa: 100<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4ece\u6587\u4ef6\u8bfb\u53d6\u5e76\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f<\/h4>\n<\/p>\n<p><p>\u540c\u6837\u5730\uff0c\u53ef\u4ee5\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6587\u672c\uff0c\u7136\u540e\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u622a\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>with open(&quot;data.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        # \u5047\u8bbe\u9700\u8981\u5339\u914d\u67d0\u4e2a\u6a21\u5f0f<\/p>\n<p>        match = re.search(r&#39;\\d+&#39;, line)<\/p>\n<p>        if match:<\/p>\n<p>            print(match.group())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u7528\u4e8e\u6570\u636e\u5206\u6790\u7684\u5f3a\u5927\u5e93\uff0c\u5c24\u5176\u9002\u5408\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u3002\u5b83\u80fd\u591f\u975e\u5e38\u65b9\u4fbf\u5730\u5bf9\u6570\u636e\u8fdb\u884c\u5207\u7247\u3001\u8fc7\u6ee4\u548c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u622a\u53d6\u5217<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8eCSV\u6587\u4ef6\uff0cPandas\u53ef\u4ee5\u76f4\u63a5\u8bfb\u53d6\u5e76\u64cd\u4f5c\u5176\u4e2d\u7684\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df = pd.read_csv(&quot;data.csv&quot;)<\/p>\n<h2><strong>\u5047\u8bbe\u9700\u8981\u622a\u53d6\u540d\u4e3a&#39;column_name&#39;\u7684\u5217<\/strong><\/h2>\n<p>sliced_column = df[&#39;column_name&#39;].str.slice(0, 5)<\/p>\n<p>print(sliced_column)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u6761\u4ef6\u8fc7\u6ee4\u548c\u5b57\u7b26\u4e32\u65b9\u6cd5<\/h4>\n<\/p>\n<p><p>Pandas\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7ed3\u5408\u6761\u4ef6\u5bf9\u6587\u672c\u5217\u8fdb\u884c\u622a\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u793a\u4f8bDataFrame<\/strong><\/h2>\n<p>data = {&#39;text_column&#39;: [&#39;abcde123&#39;, &#39;fghij456&#39;, &#39;klmno789&#39;]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u4f7f\u7528\u5b57\u7b26\u4e32\u65b9\u6cd5\u622a\u53d6<\/strong><\/h2>\n<p>df[&#39;sliced_text&#39;] = df[&#39;text_column&#39;].str.slice(0, 5)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001csv\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u7b80\u5355\u7684CSV\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>csv<\/code>\u6a21\u5757\u6765\u8bfb\u53d6\u548c\u5904\u7406\u6587\u672c\u5217\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bfb\u53d6CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528<code>csv.reader<\/code>\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u5e76\u5bf9\u6307\u5b9a\u5217\u8fdb\u884c\u622a\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&quot;data.csv&quot;, newline=&#39;&#39;) as csvfile:<\/p>\n<p>    reader = csv.reader(csvfile)<\/p>\n<p>    for row in reader:<\/p>\n<p>        # \u5047\u8bbe\u9700\u8981\u622a\u53d6\u7b2c\u4e8c\u5217\u7684\u7b2c3\u5230\u7b2c8\u4e2a\u5b57\u7b26<\/p>\n<p>        sliced_text = row[1][2:8]<\/p>\n<p>        print(sliced_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f7f\u7528\u5b57\u5178\u8bfb\u53d6\u5668<\/h4>\n<\/p>\n<p><p><code>csv.DictReader<\/code>\u53ef\u4ee5\u5c06CSV\u6587\u4ef6\u7684\u6bcf\u4e00\u884c\u89e3\u6790\u6210\u5b57\u5178\uff0c\u66f4\u65b9\u4fbf\u5730\u6839\u636e\u5217\u540d\u64cd\u4f5c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&quot;data.csv&quot;, newline=&#39;&#39;) as csvfile:<\/p>\n<p>    reader = csv.DictReader(csvfile)<\/p>\n<p>    for row in reader:<\/p>\n<p>        # \u5047\u8bbe\u9700\u8981\u622a\u53d6\u67d0\u4e2a\u5217\u540d\u5bf9\u5e94\u7684\u503c<\/p>\n<p>        sliced_text = row[&#39;column_name&#39;][2:8]<\/p>\n<p>        print(sliced_text)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u622a\u53d6\u6587\u672c\u5217\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u53ef\u4ee5\u6781\u5927\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<strong>\u5b57\u7b26\u4e32\u5207\u7247\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u9002\u5408\u5904\u7406\u7b80\u5355\u548c\u4e0d\u89c4\u5219\u7684\u6587\u672c\uff0cPandas\u5e93\u5219\u662f\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u7684\u7406\u60f3\u9009\u62e9<\/strong>\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u683c\u5f0f\uff0c\u7075\u6d3b\u8fd0\u7528\u8fd9\u4e9b\u5de5\u5177\uff0c\u5c06\u5e2e\u52a9\u4f60\u5728\u6587\u672c\u5904\u7406\u4efb\u52a1\u4e2d\u4e8b\u534a\u529f\u500d\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u622a\u53d6\u7279\u5b9a\u957f\u5ea6\u7684\u6587\u672c\u5217\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u6765\u622a\u53d6\u6587\u672c\u5217\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32<code>text = &quot;Hello, World!&quot;<\/code>\uff0c\u60f3\u8981\u622a\u53d6\u524d5\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u4f7f\u7528<code>text[:5]<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de<code>&quot;Hello&quot;<\/code>\u3002\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u7075\u6d3b\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u622a\u53d6\u4efb\u610f\u957f\u5ea6\u7684\u6587\u672c\u3002<\/p>\n<p><strong>\u5728\u5904\u7406CSV\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u622a\u53d6\u67d0\u4e00\u5217\u7684\u6587\u672c\u6570\u636e\uff1f<\/strong><br \/>\u5982\u679c\u4f60\u4f7f\u7528<code>pandas<\/code>\u5e93\u5904\u7406CSV\u6587\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>df[&#39;\u5217\u540d&#39;].str[:n]<\/code>\u6765\u622a\u53d6\u67d0\u4e00\u5217\u7684\u6587\u672c\u6570\u636e\uff0c\u5176\u4e2d<code>n<\/code>\u662f\u4f60\u5e0c\u671b\u622a\u53d6\u7684\u5b57\u7b26\u957f\u5ea6\u3002\u4f8b\u5982\uff0c<code>df[&#39;name&#39;].str[:3]<\/code>\u5c06\u8fd4\u56de\u6240\u6709\u540d\u5b57\u7684\u524d\u4e09\u4e2a\u5b57\u7b26\u3002\u8fd9\u5bf9\u4e8e\u6570\u636e\u6e05\u7406\u548c\u683c\u5f0f\u5316\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p><strong>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u5982\u4f55\u622a\u53d6\u6ee1\u8db3\u7279\u5b9a\u6a21\u5f0f\u7684\u6587\u672c\uff1f<\/strong><br \/>Python\u7684<code>re<\/code>\u6a21\u5757\u5141\u8bb8\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5339\u914d\u548c\u622a\u53d6\u6587\u672c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u8981\u4ece\u4e00\u6bb5\u6587\u672c\u4e2d\u63d0\u53d6\u6240\u6709\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\uff0c\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">import re\ntext = &quot;\u8bf7\u8054\u7cfbsupport@example.com\u6216admin@example.org&quot;\nem<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ls = re.findall(r&#39;[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+&#39;, text)\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5339\u914d\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u7684\u5217\u8868\u3002\u6b63\u5219\u8868\u8fbe\u5f0f\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u6587\u672c\u622a\u53d6\u9700\u6c42\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u622a\u53d6\u6587\u672c\u5217\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u51e0\u79cd\u65b9\u5f0f\u5b9e\u73b0\uff1a\u4f7f\u7528\u5b57\u7b26\u4e32\u5207\u7247\u3001\u5229\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3001\u8fd0\u7528Pandas\u5e93\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1005465,"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\/1005451"}],"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=1005451"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1005451\/revisions"}],"predecessor-version":[{"id":1005469,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1005451\/revisions\/1005469"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1005465"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1005451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1005451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1005451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}