{"id":1007534,"date":"2024-12-27T10:54:28","date_gmt":"2024-12-27T02:54:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1007534.html"},"modified":"2024-12-27T10:54:31","modified_gmt":"2024-12-27T02:54:31","slug":"python-%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8csv","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1007534.html","title":{"rendered":"python \u4e2d\u5982\u4f55\u4f7f\u7528CSV"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25083209\/cdbf2df4-4a7c-49b9-ac66-5ef01824534f.webp\" alt=\"python \u4e2d\u5982\u4f55\u4f7f\u7528CSV\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d\uff1a<br \/>\u5728Python\u4e2d\u4f7f\u7528CSV\u6587\u4ef6\u5341\u5206\u5e38\u89c1\uff0c\u56e0\u4e3aCSV\u6587\u4ef6\u662f\u4e00\u79cd\u7b80\u5355\u3001\u6613\u4e8e\u8bfb\u53d6\u7684\u683c\u5f0f\uff0c\u7528\u4e8e\u5b58\u50a8\u8868\u683c\u6570\u636e\u3002<strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7csv\u6a21\u5757\u3001pandas\u5e93\u3001\u548c\u5185\u7f6eopen\u51fd\u6570\u5904\u7406CSV\u6587\u4ef6<\/strong>\u3002\u5176\u4e2d\uff0ccsv\u6a21\u5757\u63d0\u4f9b\u4e86\u5bf9CSV\u6587\u4ef6\u8bfb\u5199\u7684\u57fa\u672c\u529f\u80fd\uff0cpandas\u5e93\u53ef\u4ee5\u7528\u4e8e\u66f4\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u548c\u64cd\u4f5c\uff0copen\u51fd\u6570\u5219\u662f\u5904\u7406\u6587\u4ef6\u8bfb\u5199\u7684\u57fa\u7840\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63a2\u8ba8\u6bcf\u4e00\u79cd\u65b9\u6cd5\uff0c\u5e76\u5c55\u793a\u5982\u4f55\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u4f7f\u7528\u8fd9\u4e9b\u5de5\u5177\u6765\u5904\u7406CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528CSV\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684csv\u6a21\u5757\u662f\u5904\u7406CSV\u6587\u4ef6\u7684\u6807\u51c6\u5e93\uff0c\u65e0\u9700\u5b89\u88c5\uff0c\u76f4\u63a5\u5bfc\u5165\u5373\u53ef\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bfb\u53d6CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528csv.reader()\u53ef\u4ee5\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u5e76\u4ee5\u5217\u8868\u7684\u5f62\u5f0f\u8fd4\u56de\u6bcf\u884c\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&#39;data.csv&#39;, &#39;r&#39;) as file:<\/p>\n<p>    reader = csv.reader(file)<\/p>\n<p>    for row in reader:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c<code>open<\/code>\u51fd\u6570\u4ee5\u8bfb\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c<code>csv.reader<\/code>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u8fd4\u56de\u4e00\u4e2a\u53ef\u8fed\u4ee3\u7684\u5bf9\u8c61\u3002\u6bcf\u6b21\u8fed\u4ee3\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u884c\u6570\u636e\u7684\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>2. \u5199\u5165CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528csv.writer()\u5199\u5165\u6570\u636e\u5230CSV\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>data = [[&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;], [&#39;Alice&#39;, 30, &#39;New York&#39;], [&#39;Bob&#39;, 25, &#39;Los Angeles&#39;]]<\/p>\n<p>with open(&#39;output.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    writer.writerows(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c<code>csv.writer<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u5199\u5165\u5bf9\u8c61\uff0c<code>writerows<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165CSV\u6587\u4ef6\u3002\u6ce8\u610f\u8bbe\u7f6e<code>newline=&#39;&#39;<\/code>\u4ee5\u907f\u514d\u5728Windows\u4e0a\u51fa\u73b0\u591a\u4f59\u7684\u7a7a\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u7684CSV\u6587\u4ef6\u5904\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bfb\u53d6CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528pandas\u8bfb\u53d6CSV\u6587\u4ef6\u975e\u5e38\u7b80\u4fbf\uff0c\u53ea\u9700\u8981\u4e00\u884c\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>pd.read_csv<\/code>\u51fd\u6570\u8fd4\u56de\u4e00\u4e2aDataFrame\u5bf9\u8c61\uff0c\u63d0\u4f9b\u4e86\u5bf9\u6570\u636e\u7684\u5f3a\u5927\u64cd\u4f5c\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><h4>2. \u5199\u5165CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5c06DataFrame\u5199\u5165CSV\u6587\u4ef6\u540c\u6837\u7b80\u5355\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.DataFrame({&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;], &#39;Age&#39;: [30, 25], &#39;City&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;]})<\/p>\n<p>df.to_csv(&#39;output.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><code>to_csv<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06DataFrame\u5199\u5165CSV\u6587\u4ef6\uff0c\u5e76\u901a\u8fc7<code>index=False<\/code>\u53c2\u6570\u907f\u514d\u5199\u5165\u884c\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h4>3. \u6570\u636e\u5904\u7406\u4e0e\u5206\u6790<\/h4>\n<\/p>\n<p><p>Pandas\u4e0d\u4ec5\u53ef\u4ee5\u8bfb\u53d6\u548c\u5199\u5165CSV\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u5bf9\u6570\u636e\u8fdb\u884c\u590d\u6742\u7684\u64cd\u4f5c\u548c\u5206\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u6570\u636e\u7b5b\u9009<\/strong><\/h2>\n<p>filtered_df = df[df[&#39;Age&#39;] &gt; 25]<\/p>\n<h2><strong>\u6570\u636e\u7edf\u8ba1<\/strong><\/h2>\n<p>average_age = df[&#39;Age&#39;].mean()<\/p>\n<h2><strong>\u6570\u636e\u8f6c\u6362<\/strong><\/h2>\n<p>df[&#39;Age&#39;] = df[&#39;Age&#39;].apply(lambda x: x + 1)<\/p>\n<p>print(filtered_df)<\/p>\n<p>print(&#39;Average Age:&#39;, average_age)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6211\u4eec\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528Pandas\u8fdb\u884c\u6570\u636e\u7b5b\u9009\u3001\u7edf\u8ba1\u548c\u8f6c\u6362\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Open\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u867d\u7136csv\u6a21\u5757\u548cpandas\u5e93\u5df2\u7ecf\u8db3\u591f\u5f3a\u5927\uff0c\u4f46\u6709\u65f6\u6211\u4eec\u53ef\u80fd\u9700\u8981\u4f7f\u7528Python\u7684\u5185\u7f6eopen\u51fd\u6570\u8fdb\u884c\u66f4\u5e95\u5c42\u7684\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1. \u8bfb\u53d6CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528open\u51fd\u6570\u548csplit\u65b9\u6cd5\u6765\u8bfb\u53d6CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;data.csv&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        print(line.strip().split(&#39;,&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528<code>strip<\/code>\u53bb\u9664\u6362\u884c\u7b26\uff0c<code>split<\/code>\u5c06\u6bcf\u884c\u6570\u636e\u62c6\u5206\u4e3a\u5217\u8868\u3002<\/p>\n<\/p>\n<p><h4>2. \u5199\u5165CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c\u53ef\u4ee5\u4f7f\u7528open\u51fd\u6570\u5199\u5165CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [[&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;], [&#39;Alice&#39;, 30, &#39;New York&#39;], [&#39;Bob&#39;, 25, &#39;Los Angeles&#39;]]<\/p>\n<p>with open(&#39;output.csv&#39;, &#39;w&#39;) as file:<\/p>\n<p>    for row in data:<\/p>\n<p>        file.write(&#39;,&#39;.join(map(str, row)) + &#39;\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c<code>map<\/code>\u51fd\u6570\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c<code>join<\/code>\u65b9\u6cd5\u5c06\u5217\u8868\u5408\u5e76\u4e3a\u9017\u53f7\u5206\u9694\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001CSV\u6587\u4ef6\u5904\u7406\u7684\u6700\u4f73\u5b9e\u8df5<\/h3>\n<\/p>\n<p><h4>1. \u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h4>\n<\/p>\n<p><p>Python\u7684\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff08with\u8bed\u53e5\uff09\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u4f7f\u7528\u5b8c\u540e\u6b63\u786e\u5173\u95ed\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><h4>2. \u8003\u8651\u6570\u636e\u7684\u7f16\u7801<\/h4>\n<\/p>\n<p><p>\u5728\u5904\u7406CSV\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\uff08\u5982UTF-8\uff09\uff0c\u4ee5\u907f\u514d\u8bfb\u53d6\u6216\u5199\u5165\u65f6\u51fa\u73b0\u4e71\u7801\u3002<\/p>\n<\/p>\n<p><h4>3. \u5904\u7406\u5f02\u5e38<\/h4>\n<\/p>\n<p><p>\u5728\u8bfb\u53d6\u6216\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u5f02\u5e38\uff08\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\uff09\u3002\u4f7f\u7528try-except\u8bed\u53e5\u6765\u6355\u83b7\u548c\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&#39;data.csv&#39;, &#39;r&#39;) as file:<\/p>\n<p>        # \u8bfb\u53d6\u6587\u4ef6\u64cd\u4f5c<\/p>\n<p>except FileNotFoundError:<\/p>\n<p>    print(&quot;\u6587\u4ef6\u4e0d\u5b58\u5728&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(&quot;\u53d1\u751f\u9519\u8bef:&quot;, e)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001CSV\u6587\u4ef6\u7684\u9ad8\u7ea7\u64cd\u4f5c<\/h3>\n<\/p>\n<p><h4>1. \u81ea\u5b9a\u4e49\u5206\u9694\u7b26<\/h4>\n<\/p>\n<p><p>\u867d\u7136CSV\u6587\u4ef6\u901a\u5e38\u4f7f\u7528\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u4f46\u6709\u65f6\u53ef\u80fd\u4f1a\u4f7f\u7528\u5176\u4ed6\u5b57\u7b26\uff08\u5982\u5236\u8868\u7b26\uff09\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&#39;data.tsv&#39;, &#39;r&#39;) as file:<\/p>\n<p>    reader = csv.reader(file, delimiter=&#39;\\t&#39;)<\/p>\n<p>    for row in reader:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u901a\u8fc7\u8bbe\u7f6e<code>delimiter=&#39;\\t&#39;<\/code>\u6765\u8bfb\u53d6\u5236\u8868\u7b26\u5206\u9694\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>2. \u5904\u7406\u5927\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u975e\u5e38\u5927\u7684CSV\u6587\u4ef6\uff0c\u9010\u884c\u8bfb\u53d6\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>chunk_size = 1000  # \u6bcf\u6b21\u8bfb\u53d61000\u884c<\/p>\n<p>for chunk in pd.read_csv(&#39;large_data.csv&#39;, chunksize=chunk_size):<\/p>\n<p>    process(chunk)  # \u81ea\u5b9a\u4e49\u5904\u7406\u51fd\u6570<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528<code>chunksize<\/code>\u53c2\u6570\u53ef\u4ee5\u5206\u5757\u8bfb\u53d6\u6587\u4ef6\uff0c\u6bcf\u6b21\u5904\u7406\u4e00\u5c0f\u90e8\u5206\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001CSV\u6587\u4ef6\u7684\u5b9e\u7528\u6848\u4f8b<\/h3>\n<\/p>\n<p><h4>1. \u6570\u636e\u5bfc\u5165\u4e0e\u5bfc\u51fa<\/h4>\n<\/p>\n<p><p>CSV\u6587\u4ef6\u5e38\u7528\u4e8e\u6570\u636e\u7684\u5bfc\u5165\u548c\u5bfc\u51fa\uff0c\u56e0\u4e3a\u5b83\u662f\u4e00\u79cd\u901a\u7528\u7684\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5bfc\u51fa\u6570\u636e<\/strong><\/h2>\n<p>df = pd.DataFrame({&#39;Product&#39;: [&#39;A&#39;, &#39;B&#39;, &#39;C&#39;], &#39;Price&#39;: [10, 20, 30]})<\/p>\n<p>df.to_csv(&#39;products.csv&#39;, index=False)<\/p>\n<h2><strong>\u5bfc\u5165\u6570\u636e<\/strong><\/h2>\n<p>new_df = pd.read_csv(&#39;products.csv&#39;)<\/p>\n<p>print(new_df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u6570\u636e\u6e05\u6d17<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u8fc7\u7a0b\u4e2d\uff0cCSV\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u53ef\u80fd\u9700\u8981\u6e05\u6d17\u548c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.read_csv(&#39;raw_data.csv&#39;)<\/p>\n<h2><strong>\u53bb\u9664\u7a7a\u503c<\/strong><\/h2>\n<p>df.dropna(inplace=True)<\/p>\n<h2><strong>\u53bb\u9664\u91cd\u590d\u503c<\/strong><\/h2>\n<p>df.drop_duplicates(inplace=True)<\/p>\n<h2><strong>\u6570\u636e\u683c\u5f0f\u8f6c\u6362<\/strong><\/h2>\n<p>df[&#39;Date&#39;] = pd.to_datetime(df[&#39;Date&#39;])<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u64cd\u4f5c\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5904\u7406CSV\u6587\u4ef6\u6709\u591a\u79cd\u65b9\u6cd5\uff0c\u9009\u62e9\u5177\u4f53\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4efb\u52a1\u7684\u590d\u6742\u6027\u548c\u6570\u636e\u7684\u89c4\u6a21\u3002<strong>csv\u6a21\u5757\u9002\u7528\u4e8e\u7b80\u5355\u7684\u8bfb\u5199\u64cd\u4f5c\uff0cpandas\u5e93\u5219\u9002\u5408\u590d\u6742\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406<\/strong>\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u5e94\u6ce8\u610f\u6587\u4ef6\u7684\u6b63\u786e\u5173\u95ed\u548c\u5f02\u5e38\u7684\u5904\u7406\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u548c\u53ef\u9760\u6027\u3002\u5728\u672a\u6765\uff0c\u968f\u7740\u6570\u636e\u5206\u6790\u9700\u6c42\u7684\u4e0d\u65ad\u589e\u52a0\uff0c\u5bf9CSV\u6587\u4ef6\u7684\u5904\u7406\u80fd\u529b\u4e5f\u5c06\u4e0d\u65ad\u63d0\u5347\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6CSV\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>csv<\/code>\u6a21\u5757\u6765\u8bfb\u53d6CSV\u6587\u4ef6\u3002\u9996\u5148\uff0c\u9700\u8981\u5bfc\u5165\u8be5\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>csv.reader()<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\u5e76\u8bfb\u53d6\u5185\u5bb9\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">import csv\n\nwith open(&#39;example.csv&#39;, mode=&#39;r&#39;) as file:\n    reader = csv.reader(file)\n    for row in reader:\n        print(row)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u7b80\u5355\u660e\u4e86\uff0c\u9002\u5408\u5904\u7406\u5c0f\u578bCSV\u6587\u4ef6\u3002<\/p>\n<p><strong>Python\u4e2d\u5904\u7406CSV\u6587\u4ef6\u65f6\u5982\u4f55\u6307\u5b9a\u5206\u9694\u7b26\uff1f<\/strong><br \/>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c<code>csv<\/code>\u6a21\u5757\u4f7f\u7528\u9017\u53f7\u4f5c\u4e3a\u5206\u9694\u7b26\u3002\u5982\u679cCSV\u6587\u4ef6\u4f7f\u7528\u5176\u4ed6\u5b57\u7b26\uff08\u5982\u5206\u53f7\u6216\u5236\u8868\u7b26\uff09\u4f5c\u4e3a\u5206\u9694\u7b26\uff0c\u53ef\u4ee5\u5728<code>csv.reader()<\/code>\u51fd\u6570\u4e2d\u6307\u5b9a<code>delimiter<\/code>\u53c2\u6570\u3002\u4f8b\u5982\uff0c\u5904\u7406\u5206\u53f7\u5206\u9694\u7684\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;example.csv&#39;, mode=&#39;r&#39;) as file:\n    reader = csv.reader(file, delimiter=&#39;;&#39;)\n    for row in reader:\n        print(row)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u7075\u6d3b\u6027\u4f7f\u5f97Python\u80fd\u591f\u5904\u7406\u5404\u79cd\u683c\u5f0f\u7684CSV\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u6570\u636e\u5199\u5165CSV\u6587\u4ef6\uff1f<\/strong><br \/>\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u4e0d\u4ec5\u53ef\u4ee5\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u5199\u5165\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528<code>csv.writer()<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u5199\u5165\u5668\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528<code>writerow()<\/code>\u6216<code>writerows()<\/code>\u65b9\u6cd5\u6765\u5199\u5165\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import csv\n\ndata = [[&#39;Name&#39;, &#39;Age&#39;], [&#39;Alice&#39;, 30], [&#39;Bob&#39;, 25]]\nwith open(&#39;output.csv&#39;, mode=&#39;w&#39;, newline=&#39;&#39;) as file:\n    writer = csv.writer(file)\n    writer.writerows(data)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u65b0\u7684CSV\u6587\u4ef6\uff0c\u5e76\u5c06\u6570\u636e\u5199\u5165\u5176\u4e2d\u3002\u4f7f\u7528<code>newline=&#39;&#39;<\/code>\u53c2\u6570\u53ef\u786e\u4fdd\u5728Windows\u7cfb\u7edf\u4e0a\u4e0d\u4f1a\u51fa\u73b0\u591a\u4f59\u7684\u7a7a\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d\uff1a\u5728Python\u4e2d\u4f7f\u7528CSV\u6587\u4ef6\u5341\u5206\u5e38\u89c1\uff0c\u56e0\u4e3aCSV\u6587\u4ef6\u662f\u4e00\u79cd\u7b80\u5355\u3001\u6613\u4e8e\u8bfb\u53d6\u7684\u683c\u5f0f\uff0c\u7528\u4e8e\u5b58\u50a8\u8868\u683c\u6570\u636e\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1007543,"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\/1007534"}],"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=1007534"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1007534\/revisions"}],"predecessor-version":[{"id":1007547,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1007534\/revisions\/1007547"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1007543"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1007534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1007534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1007534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}