{"id":1104142,"date":"2025-01-08T16:17:41","date_gmt":"2025-01-08T08:17:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1104142.html"},"modified":"2025-01-08T16:17:47","modified_gmt":"2025-01-08T08:17:47","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96%e4%b8%a4%e5%88%97txt%e6%96%87%e4%bb%b6%e5%90%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1104142.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6\u4e24\u5217txt\u6587\u4ef6\u540d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065634\/62086ad8-c5c5-4dec-8ccd-ce731fd1a1dc.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6\u4e24\u5217txt\u6587\u4ef6\u540d\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u53d6\u4e24\u5217txt\u6587\u4ef6\u7684\u51e0\u79cd\u65b9\u6cd5<\/strong>\uff1a\u4f7f\u7528<code>readlines()<\/code>\u65b9\u6cd5\u3001\u5229\u7528<code>pandas<\/code>\u5e93\u3001\u4f7f\u7528<code>csv<\/code>\u6a21\u5757<\/p>\n<\/p>\n<p><p>\u8be6\u7ec6\u63cf\u8ff0\uff1a<strong>\u4f7f\u7528pandas\u5e93\u5904\u7406\u6570\u636e\u66f4\u52a0\u4fbf\u6377\u4e14\u529f\u80fd\u5f3a\u5927<\/strong>\u3002Pandas\u662fPython\u4e2d\u6700\u6d41\u884c\u7684\u6570\u636e\u5904\u7406\u5e93\u4e4b\u4e00\uff0c\u5b83\u80fd\u591f\u9ad8\u6548\u5730\u8bfb\u53d6\u3001\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528readlines()\u65b9\u6cd5<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528<code>readlines()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u7684\u6240\u6709\u884c\uff0c\u5e76\u5c06\u6bcf\u884c\u5b58\u50a8\u4e3a\u5b57\u7b26\u4e32\u5217\u8868\u3002\u7136\u540e\u53ef\u4ee5\u5bf9\u6bcf\u884c\u8fdb\u884c\u5904\u7406\uff0c\u5c06\u5b57\u7b26\u4e32\u62c6\u5206\u6210\u4e24\u5217\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def read_two_columns_with_readlines(file_path):<\/p>\n<p>    column1 = []<\/p>\n<p>    column2 = []<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        lines = file.readlines()<\/p>\n<p>        for line in lines:<\/p>\n<p>            columns = line.strip().split()<\/p>\n<p>            if len(columns) &gt;= 2:<\/p>\n<p>                column1.append(columns[0])<\/p>\n<p>                column2.append(columns[1])<\/p>\n<p>    return column1, column2<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>file_path = &#39;data.txt&#39;<\/p>\n<p>col1, col2 = read_two_columns_with_readlines(file_path)<\/p>\n<p>print(col1)<\/p>\n<p>print(col2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e8c\u3001\u5229\u7528pandas\u5e93<\/h2>\n<\/p>\n<p><p>Pandas\u5e93\u63d0\u4f9b\u4e86<code>read_csv()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u5e76\u5c06\u5176\u8f6c\u6362\u4e3aDataFrame\u5bf9\u8c61\u3002\u4f7f\u7528<code>pandas.read_csv()<\/code>\u8bfb\u53d6\u4e24\u5217\u6570\u636e\u975e\u5e38\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def read_two_columns_with_pandas(file_path):<\/p>\n<p>    df = pd.read_csv(file_path, delim_whitespace=True, header=None)<\/p>\n<p>    column1 = df[0].tolist()<\/p>\n<p>    column2 = df[1].tolist()<\/p>\n<p>    return column1, column2<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>file_path = &#39;data.txt&#39;<\/p>\n<p>col1, col2 = read_two_columns_with_pandas(file_path)<\/p>\n<p>print(col1)<\/p>\n<p>print(col2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001\u4f7f\u7528csv\u6a21\u5757<\/h2>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684<code>csv<\/code>\u6a21\u5757\u4e5f\u53ef\u4ee5\u7528\u6765\u8bfb\u53d6\u4e24\u5217txt\u6587\u4ef6\u3002<code>csv.reader()<\/code>\u53ef\u4ee5\u8bfb\u53d6\u6587\u4ef6\u5e76\u6309\u884c\u8fd4\u56de\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>def read_two_columns_with_csv(file_path):<\/p>\n<p>    column1 = []<\/p>\n<p>    column2 = []<\/p>\n<p>    with open(file_path, &#39;r&#39;) as file:<\/p>\n<p>        reader = csv.reader(file, delimiter=&#39; &#39;)<\/p>\n<p>        for row in reader:<\/p>\n<p>            if len(row) &gt;= 2:<\/p>\n<p>                column1.append(row[0])<\/p>\n<p>                column2.append(row[1])<\/p>\n<p>    return column1, column2<\/p>\n<h2><strong>\u793a\u4f8b<\/strong><\/h2>\n<p>file_path = &#39;data.txt&#39;<\/p>\n<p>col1, col2 = read_two_columns_with_csv(file_path)<\/p>\n<p>print(col1)<\/p>\n<p>print(col2)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u5c0f\u7ed3<\/h2>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4e09\u79cd\u65b9\u6cd5\u5404\u6709\u4f18\u7f3a\u70b9\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u6587\u4ef6\u8bfb\u53d6\u4efb\u52a1\uff0c\u4f7f\u7528<code>readlines()<\/code>\u65b9\u6cd5\u5df2\u7ecf\u8db3\u591f\u3002\u800c\u5bf9\u4e8e\u5927\u6570\u636e\u5904\u7406\u548c\u9700\u8981\u66f4\u591a\u529f\u80fd\u7684\u573a\u666f\uff0c<strong>\u4f7f\u7528pandas\u5e93\u65e0\u7591\u662f\u6700\u4f73\u9009\u62e9<\/strong>\u3002Pandas\u4e0d\u4ec5\u80fd\u9ad8\u6548\u5730\u8bfb\u53d6\u6570\u636e\uff0c\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u529f\u80fd\u3002<code>csv<\/code>\u6a21\u5757\u9002\u7528\u4e8e\u9700\u8981\u66f4\u591a\u63a7\u5236\u7684\u573a\u666f\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u5904\u7406\u5404\u79cd\u683c\u5f0f\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u5982\u679c\u6570\u636e\u6587\u4ef6\u8f83\u5927\u4e14\u683c\u5f0f\u590d\u6742\uff0c\u63a8\u8350\u4f7f\u7528pandas\u5e93\uff1b\u5982\u679c\u53ea\u662f\u7b80\u5355\u7684\u4e24\u5217\u6570\u636e\u8bfb\u53d6\uff0c<code>readlines()<\/code>\u548c<code>csv<\/code>\u6a21\u5757\u90fd\u53ef\u4ee5\u80dc\u4efb\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u4e24\u5217\u6587\u672c\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u7ed3\u5408<code>readlines()<\/code>\u65b9\u6cd5\u6216<code>pandas<\/code>\u5e93\u6765\u8bfb\u53d6\u4e24\u5217\u7684\u6587\u672c\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u683c\u5f0f\u4e3a\u4ee5\u7a7a\u683c\u6216\u5236\u8868\u7b26\u5206\u9694\u7684\u5217\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas.read_csv()<\/code>\u51fd\u6570\uff0c\u6307\u5b9a\u5206\u9694\u7b26\u4e3a<code>sep=&#39;\\t&#39;<\/code>\u6216<code>sep=&#39; &#39;<\/code>\uff0c\u4fbf\u4e8e\u5904\u7406\u548c\u5206\u6790\u6570\u636e\u3002<\/p>\n<p><strong>\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u5904\u7406\u7a7a\u767d\u884c\u6216\u65e0\u6548\u6570\u636e\uff1f<\/strong><br \/>\u5728\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u7a7a\u767d\u884c\u6216\u65e0\u6548\u6570\u636e\u53ef\u80fd\u4f1a\u5f71\u54cd\u540e\u7eed\u7684\u6570\u636e\u5904\u7406\u3002\u4f7f\u7528<code>pandas<\/code>\u5e93\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>dropna()<\/code>\u65b9\u6cd5\u6765\u5220\u9664\u5305\u542b\u7a7a\u503c\u7684\u884c\uff0c\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u3002\u5982\u679c\u4f7f\u7528\u57fa\u672c\u7684\u6587\u4ef6\u8bfb\u53d6\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5728\u904d\u5386\u884c\u65f6\u6dfb\u52a0\u6761\u4ef6\u5224\u65ad\uff0c\u5ffd\u7565\u7a7a\u767d\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u8bfb\u53d6\u7684\u4e24\u5217\u6570\u636e\u5b58\u50a8\u5230\u5217\u8868\u6216\u5b57\u5178\u4e2d\uff1f<\/strong><br \/>\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5b58\u50a8\u5230\u5217\u8868\u6216\u5b57\u5178\u4e2d\uff0c\u65b9\u4fbf\u540e\u7eed\u64cd\u4f5c\u3002\u4f7f\u7528<code>readlines()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u6bcf\u4e00\u884c\u5b58\u50a8\u4e3a\u5217\u8868\u4e2d\u7684\u5143\u7d20\uff0c\u63a5\u7740\u4f7f\u7528<code>split()<\/code>\u51fd\u6570\u5c06\u6bcf\u884c\u7684\u4e24\u5217\u6570\u636e\u5206\u5f00\uff0c\u5e76\u5b58\u50a8\u5230\u4e00\u4e2a\u5b57\u5178\u4e2d\uff0c\u952e\u503c\u5bf9\u5f62\u5f0f\u4e3a{\u7b2c\u4e00\u5217: \u7b2c\u4e8c\u5217}\uff0c\u8fd9\u5728\u6570\u636e\u5904\u7406\u548c\u67e5\u8be2\u65f6\u5341\u5206\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u53d6\u4e24\u5217txt\u6587\u4ef6\u7684\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528readlines()\u65b9\u6cd5\u3001\u5229\u7528pandas\u5e93\u3001\u4f7f\u7528csv\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":1104150,"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\/1104142"}],"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=1104142"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104142\/revisions"}],"predecessor-version":[{"id":1104156,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104142\/revisions\/1104156"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1104150"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1104142"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1104142"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1104142"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}