{"id":1137532,"date":"2025-01-08T21:49:44","date_gmt":"2025-01-08T13:49:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1137532.html"},"modified":"2025-01-08T21:49:46","modified_gmt":"2025-01-08T13:49:46","slug":"python%e5%a6%82%e4%bd%95%e5%b0%86%e6%96%87%e6%9c%ac%e6%96%87%e4%bb%b6%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%88%97%e8%a1%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1137532.html","title":{"rendered":"python\u5982\u4f55\u5c06\u6587\u672c\u6587\u4ef6\u8f6c\u6362\u4e3a\u5217\u8868"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101332\/d3eecb86-099e-4701-9551-c5f79387adad.webp\" alt=\"python\u5982\u4f55\u5c06\u6587\u672c\u6587\u4ef6\u8f6c\u6362\u4e3a\u5217\u8868\" \/><\/p>\n<p><p> <strong>Python\u5c06\u6587\u672c\u6587\u4ef6\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5176\u4e2d\u5e38\u7528\u7684\u5305\u62ec\u4f7f\u7528readlines()\u65b9\u6cd5\u3001split()\u65b9\u6cd5\u3001\u4ee5\u53ca\u5176\u4ed6\u9ad8\u7ea7\u5904\u7406\u65b9\u5f0f\u3002\u4e0b\u9762\u662f\u8be6\u7ec6\u7684\u4ecb\u7ecd\u548c\u793a\u4f8b\uff1a<\/strong> <\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u5c06\u6587\u672c\u6587\u4ef6\u8f6c\u6362\u4e3a\u5217\u8868\u662f\u4e00\u9879\u5e38\u89c1\u4efb\u52a1\uff0c\u901a\u5e38\u7528\u4e8e\u6570\u636e\u5206\u6790\u3001\u6587\u672c\u5904\u7406\u7b49\u9886\u57df\u3002<strong>\u4f7f\u7528readlines()\u65b9\u6cd5\u3001split()\u65b9\u6cd5\u3001\u9010\u884c\u8bfb\u53d6\u5e76\u5904\u7406<\/strong>\u7b49\u65b9\u5f0f\u90fd\u53ef\u4ee5\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff0c\u5373\u4f7f\u7528readlines()\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528readlines()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528readlines()\u65b9\u6cd5\u662f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\u4e4b\u4e00\u3002\u5b83\u4f1a\u5c06\u6587\u4ef6\u4e2d\u7684\u6bcf\u4e00\u884c\u4f5c\u4e3a\u5217\u8868\u4e2d\u7684\u4e00\u4e2a\u5143\u7d20\u3002\u5177\u4f53\u5b9e\u73b0\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6253\u5f00\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528open()\u51fd\u6570\u6253\u5f00\u6587\u4ef6\u3002<\/li>\n<li><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong>\uff1a\u4f7f\u7528readlines()\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u884c\u3002<\/li>\n<li><strong>\u5173\u95ed\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528close()\u65b9\u6cd5\u5173\u95ed\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    lines = file.readlines()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>lines<\/code>\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5bf9\u5e94\u6587\u4ef6\u4e2d\u7684\u4e00\u884c\u5185\u5bb9\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8fdb\u4e00\u6b65\u63a2\u8ba8\u5176\u4ed6\u65b9\u6cd5\u548c\u8fdb\u9636\u6280\u5de7\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001\u4f7f\u7528readlines()\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5982\u4e0a\u6240\u8ff0\uff0c\u4f7f\u7528readlines()\u65b9\u6cd5\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u8fd9\u91cc\u6211\u4eec\u8fdb\u4e00\u6b65\u6269\u5c55\uff0c\u5047\u8bbe\u6587\u672c\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code>Hello, world!<\/p>\n<p>Python is great.<\/p>\n<p>File handling is easy.<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u4ee3\u7801\u5c06\u5176\u8f6c\u6362\u4e3a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    lines = file.readlines()<\/p>\n<p>    lines = [line.strip() for line in lines]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u53bb\u6389\u6bcf\u884c\u672b\u5c3e\u7684\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<p><h3>2. \u5904\u7406\u7a7a\u884c\u548c\u6ce8\u91ca\u884c<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6587\u672c\u6587\u4ef6\u53ef\u80fd\u5305\u542b\u7a7a\u884c\u548c\u6ce8\u91ca\u884c\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\u8fdb\u884c\u8fc7\u6ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    lines = file.readlines()<\/p>\n<p>    lines = [line.strip() for line in lines if line.strip() and not line.startswith(&#39;#&#39;)]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u79fb\u9664\u6240\u6709\u7a7a\u884c\u548c\u4ee5#\u5f00\u5934\u7684\u6ce8\u91ca\u884c\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e8c\u3001\u4f7f\u7528split()\u65b9\u6cd5<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u5e0c\u671b\u5c06\u6574\u4e2a\u6587\u4ef6\u5185\u5bb9\u8bfb\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u6309\u7167\u7279\u5b9a\u7684\u5206\u9694\u7b26\u8fdb\u884c\u62c6\u5206\u3002\u8fd9\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528split()\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    lines = content.split(&#39;\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>lines<\/code>\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u6bcf\u4e2a\u5143\u7d20\u5bf9\u5e94\u6587\u4ef6\u4e2d\u7684\u4e00\u884c\u3002<\/p>\n<\/p>\n<p><h3>2. \u6309\u5176\u4ed6\u5206\u9694\u7b26\u62c6\u5206<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u6587\u4ef6\u5185\u5bb9\u4f7f\u7528\u5176\u4ed6\u5206\u9694\u7b26\uff0c\u4f8b\u5982\u9017\u53f7\u6216\u7a7a\u683c\uff0c\u6211\u4eec\u53ef\u4ee5\u6307\u5b9asplit()\u65b9\u6cd5\u7684\u53c2\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    words = content.split(&#39;,&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c<code>words<\/code>\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u6bcf\u4e2a\u5143\u7d20\u5bf9\u5e94\u6587\u4ef6\u4e2d\u7528\u9017\u53f7\u5206\u9694\u7684\u90e8\u5206\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e09\u3001\u9010\u884c\u8bfb\u53d6\u5e76\u5904\u7406<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u6587\u4ef6\uff0c\u9010\u884c\u8bfb\u53d6\u5e76\u5904\u7406\u53ef\u80fd\u66f4\u52a0\u9ad8\u6548\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528for\u5faa\u73af\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = []<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        lines.append(line.strip())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u907f\u514d\u4e86\u4e00\u6b21\u6027\u5c06\u6574\u4e2a\u6587\u4ef6\u8bfb\u5165\u5185\u5b58\uff0c\u9002\u5408\u5904\u7406\u5927\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>2. \u9ad8\u7ea7\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728\u9010\u884c\u8bfb\u53d6\u7684\u8fc7\u7a0b\u4e2d\u8fdb\u884c\u5176\u4ed6\u5904\u7406\uff0c\u4f8b\u5982\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u3001\u683c\u5f0f\u5316\u5904\u7406\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>pattern = re.compile(r&#39;\\w+&#39;)<\/p>\n<p>lines = []<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        matches = pattern.findall(line)<\/p>\n<p>        if matches:<\/p>\n<p>            lines.append(matches)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u63d0\u53d6\u6bcf\u884c\u4e2d\u7684\u6240\u6709\u5355\u8bcd\uff0c\u5e76\u5c06\u5176\u6dfb\u52a0\u5230\u5217\u8868\u4e2d\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u56db\u3001\u4f7f\u7528pandas\u5e93<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u7ed3\u6784\u5316\u6587\u672c\u6587\u4ef6\uff0c\u4f8b\u5982CSV\u6587\u4ef6\uff0c\u4f7f\u7528pandas\u5e93\u53ef\u80fd\u66f4\u52a0\u65b9\u4fbf\u3002pandas\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u5e76\u4e14\u80fd\u76f4\u63a5\u5c06\u6587\u4ef6\u8bfb\u5165DataFrame\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.read_csv(&#39;example.csv&#39;)<\/p>\n<p>lines = df.values.tolist()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>lines<\/code>\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542bCSV\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u884c\uff0c\u6bcf\u884c\u662f\u4e00\u4e2a\u5b50\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>2. \u9ad8\u7ea7\u5904\u7406<\/h3>\n<\/p>\n<p><p>pandas\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u6e05\u6d17\u548c\u5904\u7406\u529f\u80fd\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u8fc7\u6ee4\u6389\u7279\u5b9a\u7684\u5217\uff0c\u6216\u8005\u8fdb\u884c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.read_csv(&#39;example.csv&#39;, usecols=[&#39;column1&#39;, &#39;column2&#39;])<\/p>\n<p>df[&#39;column1&#39;] = df[&#39;column1&#39;].astype(str)<\/p>\n<p>lines = df.values.tolist()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u8bfb\u53d6\u4e86CSV\u6587\u4ef6\u4e2d\u7684\u6307\u5b9a\u5217\uff0c\u5e76\u5c06\u67d0\u4e00\u5217\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u7c7b\u578b\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e94\u3001\u4f7f\u7528numpy\u5e93<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6570\u503c\u578b\u6570\u636e\uff0cnumpy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u7ec4\u5904\u7406\u529f\u80fd\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528numpy\u5c06\u6587\u672c\u6587\u4ef6\u8bfb\u5165\u6570\u7ec4\uff0c\u7136\u540e\u8f6c\u6362\u4e3a\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>data = np.loadtxt(&#39;example.txt&#39;)<\/p>\n<p>lines = data.tolist()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>lines<\/code>\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u6587\u672c\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u6570\u503c\u3002<\/p>\n<\/p>\n<p><h3>2. \u9ad8\u7ea7\u5904\u7406<\/h3>\n<\/p>\n<p><p>numpy\u8fd8\u652f\u6301\u66f4\u591a\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u4f8b\u5982\u6570\u636e\u5f52\u4e00\u5316\u3001\u77e9\u9635\u8fd0\u7b97\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = np.loadtxt(&#39;example.txt&#39;)<\/p>\n<p>normalized_data = (data - np.mean(data, axis=0)) \/ np.std(data, axis=0)<\/p>\n<p>lines = normalized_data.tolist()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u5bf9\u6570\u636e\u8fdb\u884c\u4e86\u6807\u51c6\u5316\u5904\u7406\uff0c\u4f7f\u5f97\u6bcf\u5217\u6570\u636e\u7684\u5747\u503c\u4e3a0\uff0c\u6807\u51c6\u5dee\u4e3a1\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u516d\u3001\u4f7f\u7528csv\u5e93<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eCSV\u6587\u4ef6\uff0cPython\u7684\u5185\u7f6ecsv\u5e93\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u8bfb\u53d6\u548c\u5199\u5165\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&#39;example.csv&#39;, &#39;r&#39;) as file:<\/p>\n<p>    reader = csv.reader(file)<\/p>\n<p>    lines = [row for row in reader]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>lines<\/code>\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542bCSV\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u884c\uff0c\u6bcf\u884c\u662f\u4e00\u4e2a\u5b50\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h3>2. \u9ad8\u7ea7\u5904\u7406<\/h3>\n<\/p>\n<p><p>csv\u5e93\u8fd8\u652f\u6301\u66f4\u591a\u7684\u81ea\u5b9a\u4e49\u9009\u9879\uff0c\u4f8b\u5982\u6307\u5b9a\u5206\u9694\u7b26\u3001\u5904\u7406\u5f15\u53f7\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.csv&#39;, &#39;r&#39;) as file:<\/p>\n<p>    reader = csv.reader(file, delimiter=&#39;;&#39;, quotechar=&#39;&quot;&#39;)<\/p>\n<p>    lines = [row for row in reader]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528\u5206\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u5e76\u5904\u7406\u4e86\u5f15\u53f7\u4e2d\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e03\u3001\u4f7f\u7528json\u5e93<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eJSON\u683c\u5f0f\u7684\u6587\u4ef6\uff0cPython\u7684\u5185\u7f6ejson\u5e93\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u89e3\u6790\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>with open(&#39;example.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<p>    lines = data[&#39;lines&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>lines<\/code>\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542bJSON\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u884c\u3002<\/p>\n<\/p>\n<p><h3>2. \u9ad8\u7ea7\u5904\u7406<\/h3>\n<\/p>\n<p><p>json\u5e93\u8fd8\u652f\u6301\u66f4\u591a\u7684\u81ea\u5b9a\u4e49\u9009\u9879\uff0c\u4f8b\u5982\u5904\u7406\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<p>    lines = [item[&#39;field&#39;] for item in data[&#39;lines&#39;]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u63d0\u53d6\u4e86JSON\u6587\u4ef6\u4e2d\u5d4c\u5957\u7ed3\u6784\u4e2d\u7684\u7279\u5b9a\u5b57\u6bb5\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u516b\u3001\u4f7f\u7528yaml\u5e93<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eYAML\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528PyYAML\u5e93\u8fdb\u884c\u89e3\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yaml<\/p>\n<p>with open(&#39;example.yaml&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = yaml.<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_load(file)<\/p>\n<p>    lines = data[&#39;lines&#39;]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>lines<\/code>\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542bYAML\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u884c\u3002<\/p>\n<\/p>\n<p><h3>2. \u9ad8\u7ea7\u5904\u7406<\/h3>\n<\/p>\n<p><p>PyYAML\u5e93\u8fd8\u652f\u6301\u66f4\u591a\u7684\u81ea\u5b9a\u4e49\u9009\u9879\uff0c\u4f8b\u5982\u5904\u7406\u590d\u6742\u7684\u5d4c\u5957\u7ed3\u6784\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.yaml&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = yaml.safe_load(file)<\/p>\n<p>    lines = [item[&#39;field&#39;] for item in data[&#39;lines&#39;]]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u63d0\u53d6\u4e86YAML\u6587\u4ef6\u4e2d\u5d4c\u5957\u7ed3\u6784\u4e2d\u7684\u7279\u5b9a\u5b57\u6bb5\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u4e5d\u3001\u4f7f\u7528configparser\u5e93<\/h2>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u914d\u7f6e\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528configparser\u5e93\u8fdb\u884c\u89e3\u6790\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import configparser<\/p>\n<p>config = configparser.ConfigParser()<\/p>\n<p>config.read(&#39;example.ini&#39;)<\/p>\n<p>lines = config.sections()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c<code>lines<\/code>\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u5305\u542b\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u6240\u6709\u90e8\u5206\u3002<\/p>\n<\/p>\n<p><h3>2. \u9ad8\u7ea7\u5904\u7406<\/h3>\n<\/p>\n<p><p>configparser\u5e93\u8fd8\u652f\u6301\u66f4\u591a\u7684\u81ea\u5b9a\u4e49\u9009\u9879\uff0c\u4f8b\u5982\u5904\u7406\u7279\u5b9a\u7684\u952e\u503c\u5bf9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = []<\/p>\n<p>for section in config.sections():<\/p>\n<p>    for key, value in config.items(section):<\/p>\n<p>        lines.append(f&quot;{key}: {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u63d0\u53d6\u4e86\u914d\u7f6e\u6587\u4ef6\u4e2d\u6240\u6709\u7684\u952e\u503c\u5bf9\u3002<\/p>\n<\/p>\n<hr>\n<p><h2>\u5341\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u5c06\u6587\u672c\u6587\u4ef6\u8f6c\u6362\u4e3a\u5217\u8868\u5728Python\u4e2d\u6709\u591a\u79cd\u5b9e\u73b0\u65b9\u6cd5\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u6587\u4ef6\u683c\u5f0f\u3002<strong>readlines()\u65b9\u6cd5\u3001split()\u65b9\u6cd5\u3001\u9010\u884c\u8bfb\u53d6\u5e76\u5904\u7406<\/strong>\u662f\u6700\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u800c\u5bf9\u4e8e\u7ed3\u6784\u5316\u6570\u636e\uff0c\u4f7f\u7528pandas\u3001numpy\u3001csv\u3001json\u3001yaml\u7b49\u5e93\u5219\u4f1a\u66f4\u52a0\u9ad8\u6548\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u548c\u7ec4\u5408\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u5404\u79cd\u7c7b\u578b\u7684\u6587\u672c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u5e76\u5c06\u5176\u5185\u5bb9\u5b58\u50a8\u4e3a\u5217\u8868\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570\u6765\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\uff0c\u5c06\u6bcf\u4e00\u884c\u4f5c\u4e3a\u5217\u8868\u7684\u4e00\u4e2a\u5143\u7d20\u3002\u901a\u8fc7<code>open()<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u7ed3\u5408<code>readlines()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;filename.txt&#39;, &#39;r&#39;) as file:\n    lines = file.readlines()\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u6bcf\u4e00\u884c\u5185\u5bb9\u5b58\u50a8\u5728<code>lines<\/code>\u5217\u8868\u4e2d\u3002\u786e\u4fdd\u5728\u5904\u7406\u6587\u4ef6\u65f6\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\uff0c\u4ee5\u4fbf\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\u3002<\/p>\n<hr>\n<p><strong>\u5982\u4f55\u5904\u7406\u6587\u672c\u6587\u4ef6\u4e2d\u7684\u7a7a\u884c\u6216\u7279\u5b9a\u683c\u5f0f\u7684\u884c\uff1f<\/strong><br \/>\u5728\u5c06\u6587\u672c\u6587\u4ef6\u5185\u5bb9\u8f6c\u6362\u4e3a\u5217\u8868\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u7a7a\u884c\u6216\u7279\u5b9a\u683c\u5f0f\u7684\u884c\u3002\u4e3a\u4e86\u8fc7\u6ee4\u6389\u8fd9\u4e9b\u884c\uff0c\u53ef\u4ee5\u5728\u8bfb\u53d6\u6587\u4ef6\u65f6\u52a0\u5165\u6761\u4ef6\u5224\u65ad\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;filename.txt&#39;, &#39;r&#39;) as file:\n    lines = [line.strip() for line in file if line.strip()]\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\uff0c\u53bb\u9664\u4e86\u7a7a\u884c\uff0c\u5e76\u901a\u8fc7<code>strip()<\/code>\u65b9\u6cd5\u79fb\u9664\u4e86\u6bcf\u884c\u7684\u524d\u540e\u7a7a\u683c\u3002<\/p>\n<hr>\n<p><strong>\u5982\u4f55\u5c06\u6587\u672c\u6587\u4ef6\u7684\u6bcf\u4e00\u884c\u8f6c\u6362\u4e3a\u7279\u5b9a\u7684\u6570\u636e\u7c7b\u578b\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5c06\u6587\u672c\u6587\u4ef6\u4e2d\u6bcf\u4e00\u884c\u7684\u6570\u636e\u8f6c\u6362\u4e3a\u7279\u5b9a\u7c7b\u578b\uff0c\u4f8b\u5982\u6574\u6570\u6216\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u5728\u8bfb\u53d6\u65f6\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u5c06\u6bcf\u884c\u8f6c\u6362\u4e3a\u6574\u6570\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;filename.txt&#39;, &#39;r&#39;) as file:\n    numbers = [int(line.strip()) for line in file if line.strip().isdigit()]\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u786e\u4fdd\u53ea\u5904\u7406\u5305\u542b\u6570\u5b57\u7684\u884c\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a\u6574\u6570\uff0c\u6700\u7ec8\u5b58\u50a8\u5728<code>numbers<\/code>\u5217\u8868\u4e2d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5c06\u6587\u672c\u6587\u4ef6\u8f6c\u6362\u4e3a\u5217\u8868\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5176\u4e2d\u5e38\u7528\u7684\u5305\u62ec\u4f7f\u7528readlines()\u65b9\u6cd5\u3001split()\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1137538,"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\/1137532"}],"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=1137532"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1137532\/revisions"}],"predecessor-version":[{"id":1137543,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1137532\/revisions\/1137543"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1137538"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1137532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1137532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1137532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}