{"id":1152827,"date":"2025-01-13T17:30:41","date_gmt":"2025-01-13T09:30:41","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1152827.html"},"modified":"2025-01-13T17:30:43","modified_gmt":"2025-01-13T09:30:43","slug":"python%e5%a6%82%e4%bd%95%e5%bc%95%e5%85%a5csv%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1152827.html","title":{"rendered":"Python\u5982\u4f55\u5f15\u5165csv\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25182712\/64769ddb-8c98-42ed-aafb-a4513369f7fb.webp\" alt=\"Python\u5982\u4f55\u5f15\u5165csv\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u5f15\u5165CSV\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5185\u7f6eCSV\u6a21\u5757\u3001\u4f7f\u7528Pandas\u5e93\u3001\u4f7f\u7528Numpy\u5e93\u3001\u4f7f\u7528DictReader\u7b49\u3002\u5176\u4e2d\uff0c\u4f7f\u7528Pandas\u5e93\u662f\u6700\u65b9\u4fbf\u548c\u5f3a\u5927\u7684\u65b9\u6cd5\u3002<\/strong> \u4f7f\u7528Pandas\u5e93\u4e0d\u4ec5\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002\u4e0b\u9762\u8be6\u7ec6\u63cf\u8ff0\u4f7f\u7528Pandas\u5e93\u5f15\u5165CSV\u6587\u4ef6\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u5e93\u5f15\u5165CSV\u6587\u4ef6\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u51e0\u884c\u4ee3\u7801\u3002\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Pandas\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c\u547d\u4ee4<code>pip install pandas<\/code>\u6765\u5b89\u88c5\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>pandas.read_csv()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6CSV\u6587\u4ef6\u3002\u8fd9\u4e2a\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u5404\u79cd\u6570\u636e\u5904\u7406\uff0c\u4f8b\u5982\u8bbe\u7f6e\u5217\u540d\u3001\u5904\u7406\u7f3a\u5931\u503c\u3001\u6307\u5b9a\u6570\u636e\u7c7b\u578b\u7b49\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(&#39;yourfile.csv&#39;)<\/p>\n<h2><strong>\u67e5\u770b\u524d\u4e94\u884c\u6570\u636e<\/strong><\/h2>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5bfc\u5165Pandas\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>read_csv()<\/code>\u51fd\u6570\u8bfb\u53d6CSV\u6587\u4ef6\u3002\u8bfb\u53d6\u7684\u6570\u636e\u5c06\u5b58\u50a8\u5728\u4e00\u4e2aDataFrame\u5bf9\u8c61\u4e2d\uff0c\u8fd9\u662f\u4e00\u79cd\u9002\u7528\u4e8e\u6570\u636e\u5206\u6790\u7684\u4e8c\u7ef4\u8868\u7ed3\u6784\u3002\u6700\u540e\uff0c\u4f7f\u7528<code>head()<\/code>\u51fd\u6570\u67e5\u770b\u524d\u4e94\u884c\u6570\u636e\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528CSV\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6eCSV\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u8bfb\u53d6\u548c\u5199\u5165CSV\u6587\u4ef6\u3002\u8fd9\u4e2a\u6a21\u5757\u975e\u5e38\u9002\u5408\u5904\u7406\u5c0f\u578b\u6570\u636e\u96c6\uff0c\u4f46\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\u53ef\u80fd\u4e0d\u591f\u9ad8\u6548\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528CSV\u6a21\u5757\u8bfb\u53d6CSV\u6587\u4ef6\u7684\u57fa\u672c\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&#39;yourfile.csv&#39;, mode=&#39;r&#39;) as file:<\/p>\n<p>    csv_reader = csv.reader(file)<\/p>\n<p>    for row in csv_reader:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5bfc\u5165CSV\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u6253\u5f00CSV\u6587\u4ef6\u3002<code>csv.reader()<\/code>\u51fd\u6570\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u8bfb\u53d6\u5668\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u5c06\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u6700\u540e\uff0c\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u6765\u6253\u5370\u6bcf\u4e00\u884c\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>1.1\u3001\u5904\u7406\u4e0d\u540c\u7684\u5206\u9694\u7b26<\/h4>\n<\/p>\n<p><p>CSV\u6587\u4ef6\u7684\u9ed8\u8ba4\u5206\u9694\u7b26\u662f\u9017\u53f7\uff0c\u4f46\u6709\u65f6\u6587\u4ef6\u53ef\u80fd\u4f7f\u7528\u5176\u4ed6\u5206\u9694\u7b26\uff0c\u4f8b\u5982\u5236\u8868\u7b26\u6216\u5206\u53f7\u3002\u4f60\u53ef\u4ee5\u5728<code>csv.reader()<\/code>\u51fd\u6570\u4e2d\u6307\u5b9a\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;yourfile.csv&#39;, mode=&#39;r&#39;) as file:<\/p>\n<p>    csv_reader = csv.reader(file, delimiter=&#39;;&#39;)<\/p>\n<p>    for row in csv_reader:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>delimiter<\/code>\u53c2\u6570\u6307\u5b9a\u5206\u9694\u7b26\u4e3a\u5206\u53f7\u3002<\/p>\n<\/p>\n<p><h4>1.2\u3001\u8bfb\u53d6\u5e26\u6709\u6807\u9898\u884c\u7684CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u8bb8\u591aCSV\u6587\u4ef6\u7684\u7b2c\u4e00\u884c\u662f\u6807\u9898\u884c\uff0c\u5305\u542b\u5217\u540d\u3002\u53ef\u4ee5\u4f7f\u7528<code>next()<\/code>\u51fd\u6570\u8df3\u8fc7\u6807\u9898\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;yourfile.csv&#39;, mode=&#39;r&#39;) as file:<\/p>\n<p>    csv_reader = csv.reader(file)<\/p>\n<p>    header = next(csv_reader)<\/p>\n<p>    print(f&#39;Column names are {&quot;, &quot;.join(header)}&#39;)<\/p>\n<p>    for row in csv_reader:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>next()<\/code>\u51fd\u6570\u8bfb\u53d6\u5e76\u6253\u5370\u6807\u9898\u884c\uff0c\u7136\u540e\u7ee7\u7eed\u8bfb\u53d6\u5269\u4f59\u7684\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u5de5\u5177\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u3002\u4f7f\u7528Pandas\u5e93\u8bfb\u53d6CSV\u6587\u4ef6\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u51e0\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;yourfile.csv&#39;)<\/p>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5bfc\u5165Pandas\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>read_csv()<\/code>\u51fd\u6570\u8bfb\u53d6CSV\u6587\u4ef6\u3002\u8bfb\u53d6\u7684\u6570\u636e\u5c06\u5b58\u50a8\u5728\u4e00\u4e2aDataFrame\u5bf9\u8c61\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>head()<\/code>\u51fd\u6570\u67e5\u770b\u524d\u4e94\u884c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>2.1\u3001\u6307\u5b9a\u5217\u540d<\/h4>\n<\/p>\n<p><p>\u6709\u65f6CSV\u6587\u4ef6\u53ef\u80fd\u6ca1\u6709\u6807\u9898\u884c\uff0c\u6216\u8005\u4f60\u5e0c\u671b\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u5217\u540d\u3002\u53ef\u4ee5\u5728<code>read_csv()<\/code>\u51fd\u6570\u4e2d\u4f7f\u7528<code>names<\/code>\u53c2\u6570\u6307\u5b9a\u5217\u540d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.read_csv(&#39;yourfile.csv&#39;, names=[&#39;col1&#39;, &#39;col2&#39;, &#39;col3&#39;])<\/p>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>names<\/code>\u53c2\u6570\u6307\u5b9a\u5217\u540d\u4e3a<code>col1<\/code>\u3001<code>col2<\/code>\u548c<code>col3<\/code>\u3002<\/p>\n<\/p>\n<p><h4>2.2\u3001\u5904\u7406\u7f3a\u5931\u503c<\/h4>\n<\/p>\n<p><p>CSV\u6587\u4ef6\u4e2d\u53ef\u80fd\u5305\u542b\u7f3a\u5931\u503c\uff0c\u8fd9\u4e9b\u503c\u901a\u5e38\u8868\u793a\u4e3a\u7a7a\u5b57\u7b26\u4e32\u6216\u7279\u5b9a\u7684\u6807\u8bb0\uff08\u4f8b\u5982<code>NA<\/code>\u3001<code>NULL<\/code>\u7b49\uff09\u3002\u53ef\u4ee5\u4f7f\u7528<code>na_values<\/code>\u53c2\u6570\u6307\u5b9a\u8fd9\u4e9b\u6807\u8bb0\uff0c\u5e76\u4f7f\u7528<code>dropna()<\/code>\u51fd\u6570\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.read_csv(&#39;yourfile.csv&#39;, na_values=[&#39;NA&#39;, &#39;NULL&#39;])<\/p>\n<p>df.dropna(inplace=True)<\/p>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>na_values<\/code>\u53c2\u6570\u6307\u5b9a\u7f3a\u5931\u503c\u6807\u8bb0\u4e3a<code>NA<\/code>\u548c<code>NULL<\/code>\uff0c\u5e76\u4f7f\u7528<code>dropna()<\/code>\u51fd\u6570\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528Numpy\u5e93<\/h3>\n<\/p>\n<p><p>Numpy\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5e93\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u578b\u6570\u7ec4\u548c\u77e9\u9635\u3002\u867d\u7136Numpy\u5e93\u4e0d\u5982Pandas\u5e93\u90a3\u4e48\u65b9\u4fbf\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u53ef\u80fd\u66f4\u9ad8\u6548\u3002\u53ef\u4ee5\u4f7f\u7528<code>numpy.genfromtxt()<\/code>\u51fd\u6570\u8bfb\u53d6CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>data = np.genfromtxt(&#39;yourfile.csv&#39;, delimiter=&#39;,&#39;, skip_header=1)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5bfc\u5165Numpy\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>genfromtxt()<\/code>\u51fd\u6570\u8bfb\u53d6CSV\u6587\u4ef6\u3002<code>delimiter<\/code>\u53c2\u6570\u6307\u5b9a\u5206\u9694\u7b26\u4e3a\u9017\u53f7\uff0c<code>skip_header<\/code>\u53c2\u6570\u7528\u4e8e\u8df3\u8fc7\u6807\u9898\u884c\u3002<\/p>\n<\/p>\n<p><h4>3.1\u3001\u6307\u5b9a\u6570\u636e\u7c7b\u578b<\/h4>\n<\/p>\n<p><p>Numpy\u5e93\u5141\u8bb8\u4f60\u6307\u5b9a\u8bfb\u53d6\u6570\u636e\u7684\u7c7b\u578b\u3002\u53ef\u4ee5\u4f7f\u7528<code>dtype<\/code>\u53c2\u6570\u6307\u5b9a\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = np.genfromtxt(&#39;yourfile.csv&#39;, delimiter=&#39;,&#39;, dtype=None, encoding=None)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>dtype<\/code>\u53c2\u6570\u6307\u5b9a\u6570\u636e\u7c7b\u578b\u4e3a\u81ea\u52a8\u68c0\u6d4b\u3002<\/p>\n<\/p>\n<p><h4>3.2\u3001\u5904\u7406\u7f3a\u5931\u503c<\/h4>\n<\/p>\n<p><p>Numpy\u5e93\u540c\u6837\u5141\u8bb8\u4f60\u5904\u7406\u7f3a\u5931\u503c\u3002\u53ef\u4ee5\u4f7f\u7528<code>missing_values<\/code>\u548c<code>filling_values<\/code>\u53c2\u6570\u6307\u5b9a\u7f3a\u5931\u503c\u6807\u8bb0\u548c\u586b\u5145\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = np.genfromtxt(&#39;yourfile.csv&#39;, delimiter=&#39;,&#39;, skip_header=1, missing_values=&#39;NA&#39;, filling_values=0)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>missing_values<\/code>\u53c2\u6570\u6307\u5b9a\u7f3a\u5931\u503c\u6807\u8bb0\u4e3a<code>NA<\/code>\uff0c\u5e76\u4f7f\u7528<code>filling_values<\/code>\u53c2\u6570\u5c06\u7f3a\u5931\u503c\u586b\u5145\u4e3a0\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528DictReader<\/h3>\n<\/p>\n<p><p>CSV\u6a21\u5757\u7684<code>DictReader<\/code>\u7c7b\u5141\u8bb8\u4f60\u5c06CSV\u6587\u4ef6\u8bfb\u53d6\u4e3a\u5b57\u5178\u683c\u5f0f\uff0c\u6bcf\u4e00\u884c\u6570\u636e\u5bf9\u5e94\u4e00\u4e2a\u5b57\u5178\u3002\u8fd9\u6837\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8bbf\u95ee\u6570\u636e\uff0c\u7279\u522b\u662f\u5f53CSV\u6587\u4ef6\u5305\u542b\u6807\u9898\u884c\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&#39;yourfile.csv&#39;, mode=&#39;r&#39;) as file:<\/p>\n<p>    csv_reader = csv.DictReader(file)<\/p>\n<p>    for row in csv_reader:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u5bfc\u5165CSV\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>DictReader<\/code>\u7c7b\u8bfb\u53d6CSV\u6587\u4ef6\u3002\u6bcf\u4e00\u884c\u6570\u636e\u5c06\u5b58\u50a8\u5728\u4e00\u4e2a\u5b57\u5178\u4e2d\uff0c\u5b57\u5178\u7684\u952e\u4e3a\u5217\u540d\u3002<\/p>\n<\/p>\n<p><h4>4.1\u3001\u5904\u7406\u4e0d\u540c\u7684\u5206\u9694\u7b26<\/h4>\n<\/p>\n<p><p>\u540c\u6837\uff0c<code>DictReader<\/code>\u7c7b\u5141\u8bb8\u4f60\u6307\u5b9a\u5206\u9694\u7b26\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;yourfile.csv&#39;, mode=&#39;r&#39;) as file:<\/p>\n<p>    csv_reader = csv.DictReader(file, delimiter=&#39;;&#39;)<\/p>\n<p>    for row in csv_reader:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>delimiter<\/code>\u53c2\u6570\u6307\u5b9a\u5206\u9694\u7b26\u4e3a\u5206\u53f7\u3002<\/p>\n<\/p>\n<p><h4>4.2\u3001\u8bfb\u53d6\u6307\u5b9a\u5217<\/h4>\n<\/p>\n<p><p>\u6709\u65f6\u4f60\u53ef\u80fd\u53ea\u9700\u8981\u8bfb\u53d6\u7279\u5b9a\u7684\u5217\u3002\u53ef\u4ee5\u5728<code>DictReader<\/code>\u7c7b\u4e2d\u4f7f\u7528<code>fieldnames<\/code>\u53c2\u6570\u6307\u5b9a\u8981\u8bfb\u53d6\u7684\u5217\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;yourfile.csv&#39;, mode=&#39;r&#39;) as file:<\/p>\n<p>    csv_reader = csv.DictReader(file, fieldnames=[&#39;col1&#39;, &#39;col2&#39;])<\/p>\n<p>    for row in csv_reader:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>fieldnames<\/code>\u53c2\u6570\u6307\u5b9a\u53ea\u8bfb\u53d6<code>col1<\/code>\u548c<code>col2<\/code>\u5217\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u5927\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u5185\u5b58\u7ba1\u7406\u53d8\u5f97\u975e\u5e38\u91cd\u8981\u3002\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c\u6700\u597d\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\uff0c\u800c\u4e0d\u662f\u4e00\u6b21\u6027\u5c06\u6574\u4e2a\u6587\u4ef6\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<p><h4>5.1\u3001\u4f7f\u7528Pandas\u9010\u884c\u8bfb\u53d6<\/h4>\n<\/p>\n<p><p>Pandas\u7684<code>read_csv()<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86<code>chunksize<\/code>\u53c2\u6570\uff0c\u4f7f\u4f60\u53ef\u4ee5\u9010\u5757\u8bfb\u53d6\u6587\u4ef6\u3002\u6bcf\u4e00\u5757\u6570\u636e\u5c06\u4f5c\u4e3a\u4e00\u4e2aDataFrame\u8fd4\u56de\uff0c\u4f60\u53ef\u4ee5\u5bf9\u6bcf\u4e2aDataFrame\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>chunk_size = 1000<\/p>\n<p>chunks = pd.read_csv(&#39;yourfile.csv&#39;, chunksize=chunk_size)<\/p>\n<p>for chunk in chunks:<\/p>\n<p>    print(chunk.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>chunksize<\/code>\u53c2\u6570\u6307\u5b9a\u6bcf\u5757\u6570\u636e\u7684\u5927\u5c0f\u4e3a1000\u884c\u3002<\/p>\n<\/p>\n<p><h4>5.2\u3001\u4f7f\u7528CSV\u6a21\u5757\u9010\u884c\u8bfb\u53d6<\/h4>\n<\/p>\n<p><p>CSV\u6a21\u5757\u7684\u8bfb\u53d6\u5668\u5bf9\u8c61\u672c\u8eab\u5c31\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u56e0\u6b64\u4f60\u53ef\u4ee5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&#39;yourfile.csv&#39;, mode=&#39;r&#39;) as file:<\/p>\n<p>    csv_reader = csv.reader(file)<\/p>\n<p>    for row in csv_reader:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5199\u5165CSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u6709\u65f6\u4f60\u8fd8\u9700\u8981\u5c06\u6570\u636e\u5199\u5165CSV\u6587\u4ef6\u3002Python\u7684CSV\u6a21\u5757\u548cPandas\u5e93\u90fd\u63d0\u4f9b\u4e86\u5199\u5165CSV\u6587\u4ef6\u7684\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>6.1\u3001\u4f7f\u7528CSV\u6a21\u5757\u5199\u5165<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528CSV\u6a21\u5757\u7684<code>writer<\/code>\u7c7b\u5c06\u6570\u636e\u5199\u5165CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>data = [<\/p>\n<p>    [&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;],<\/p>\n<p>    [&#39;Alice&#39;, 30, &#39;New York&#39;],<\/p>\n<p>    [&#39;Bob&#39;, 25, &#39;Los Angeles&#39;]<\/p>\n<p>]<\/p>\n<p>with open(&#39;output.csv&#39;, mode=&#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    csv_writer = csv.writer(file)<\/p>\n<p>    csv_writer.writerows(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528<code>writer<\/code>\u7c7b\u521b\u5efa\u4e00\u4e2a\u5199\u5165\u5668\u5bf9\u8c61\uff0c\u5e76\u4f7f\u7528<code>writerows()<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>6.2\u3001\u4f7f\u7528Pandas\u5199\u5165<\/h4>\n<\/p>\n<p><p>Pandas\u5e93\u63d0\u4f9b\u4e86<code>to_csv()<\/code>\u51fd\u6570\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06DataFrame\u5199\u5165CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;],<\/p>\n<p>    &#39;Age&#39;: [30, 25],<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;Los Angeles&#39;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<p>df.to_csv(&#39;output.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u521b\u5efa\u4e00\u4e2aDataFrame\u5bf9\u8c61\uff0c\u7136\u540e\u4f7f\u7528<code>to_csv()<\/code>\u51fd\u6570\u5c06\u5176\u5199\u5165CSV\u6587\u4ef6\u3002<code>index<\/code>\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u662f\u5426\u5199\u5165\u884c\u7d22\u5f15\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5f15\u5165\u548c\u5904\u7406CSV\u6587\u4ef6\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684CSV\u6a21\u5757\u3001Pandas\u5e93\u3001Numpy\u5e93\u4ee5\u53caDictReader\u7c7b\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u666f\u3002\u5bf9\u4e8e\u5927\u591a\u6570\u6570\u636e\u5206\u6790\u4efb\u52a1\uff0c\u4f7f\u7528Pandas\u5e93\u662f\u6700\u65b9\u4fbf\u548c\u5f3a\u5927\u7684\u9009\u62e9\u3002\u5bf9\u4e8e\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u7684\u5206\u5757\u8bfb\u53d6\u529f\u80fd\u6216CSV\u6a21\u5757\u7684\u9010\u884c\u8bfb\u53d6\u529f\u80fd\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u5c06\u4f7f\u4f60\u80fd\u591f\u66f4\u9ad8\u6548\u5730\u5904\u7406CSV\u6587\u4ef6\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\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>csv<\/code>\u6a21\u5757\u6216<code>pandas<\/code>\u5e93\u6765\u8bfb\u53d6CSV\u6587\u4ef6\u3002\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>csv.reader()<\/code>\u65b9\u6cd5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u3002\u800c\u4f7f\u7528<code>pandas<\/code>\u5e93\u5219\u53ef\u4ee5\u901a\u8fc7<code>pandas.read_csv()<\/code>\u51fd\u6570\u4e00\u6b21\u6027\u52a0\u8f7d\u6574\u4e2aCSV\u6587\u4ef6\u4e3aDataFrame\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u3002\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\u53d6\u51b3\u4e8e\u4f60\u7684\u9700\u6c42\u548c\u6570\u636e\u7684\u590d\u6742\u6027\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5904\u7406CSV\u6587\u4ef6\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u9519\u8bef\u9700\u8981\u6ce8\u610f\uff1f<\/strong><br \/>\u5904\u7406CSV\u6587\u4ef6\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u6587\u4ef6\u8def\u5f84\u4e0d\u6b63\u786e\u3001\u6587\u4ef6\u683c\u5f0f\u4e0d\u7b26\u5408\u9884\u671f\u3001\u7f3a\u5931\u503c\u5904\u7406\u4e0d\u5f53\u7b49\u3002\u4f8b\u5982\uff0c\u5f53\u6587\u4ef6\u8def\u5f84\u9519\u8bef\u65f6\uff0cPython\u4f1a\u629b\u51fa<code>FileNotFoundError<\/code>\u3002\u786e\u4fdd\u6587\u4ef6\u5b58\u5728\u5e76\u4e14\u8def\u5f84\u6b63\u786e\u662f\u907f\u514d\u6b64\u7c7b\u9519\u8bef\u7684\u5173\u952e\u3002\u6b64\u5916\uff0c\u4e86\u89e3\u5982\u4f55\u5904\u7406\u7f3a\u5931\u503c\u548c\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e5f\u975e\u5e38\u91cd\u8981\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u51c6\u786e\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u5904\u7406\u540e\u7684\u6570\u636e\u4fdd\u5b58\u56deCSV\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u7684<code>csv.writer()<\/code>\u65b9\u6cd5\u6216<code>pandas<\/code>\u5e93\u7684<code>to_csv()<\/code>\u65b9\u6cd5\u5c06\u5904\u7406\u540e\u7684\u6570\u636e\u5199\u56deCSV\u6587\u4ef6\u3002\u4f7f\u7528<code>csv.writer()<\/code>\u65f6\uff0c\u9700\u8981\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u5e76\u9010\u884c\u5199\u5165\u6570\u636e\uff0c\u800c\u4f7f\u7528<code>pandas<\/code>\u7684<code>to_csv()<\/code>\u65b9\u6cd5\u5219\u53ef\u4ee5\u76f4\u63a5\u5c06DataFrame\u4fdd\u5b58\u4e3aCSV\u6587\u4ef6\u3002\u786e\u4fdd\u5728\u4fdd\u5b58\u65f6\u6307\u5b9a\u6b63\u786e\u7684\u6587\u4ef6\u8def\u5f84\u548c\u540d\u79f0\uff0c\u4ee5\u907f\u514d\u8986\u76d6\u91cd\u8981\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5f15\u5165CSV\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\uff1a\u4f7f\u7528\u5185\u7f6eCSV\u6a21\u5757\u3001\u4f7f\u7528Pandas\u5e93\u3001\u4f7f\u7528Numpy\u5e93\u3001\u4f7f\u7528DictRe [&hellip;]","protected":false},"author":3,"featured_media":1152834,"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\/1152827"}],"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=1152827"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1152827\/revisions"}],"predecessor-version":[{"id":1152835,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1152827\/revisions\/1152835"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1152834"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1152827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1152827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1152827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}