{"id":1116684,"date":"2025-01-08T18:18:46","date_gmt":"2025-01-08T10:18:46","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1116684.html"},"modified":"2025-01-08T18:18:48","modified_gmt":"2025-01-08T10:18:48","slug":"python%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5%e4%b8%ad%e6%96%87%e5%90%8d%e5%ad%97%e7%9a%84excel-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1116684.html","title":{"rendered":"python\u5982\u4f55\u5bfc\u5165\u4e2d\u6587\u540d\u5b57\u7684excel"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080915\/196407f7-547b-44fc-a267-04ad8a80f181.webp\" alt=\"python\u5982\u4f55\u5bfc\u5165\u4e2d\u6587\u540d\u5b57\u7684excel\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u5bfc\u5165\u4e2d\u6587\u540d\u5b57\u7684Excel\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u3002<\/strong>\u3001<strong>\u786e\u4fdd\u6587\u4ef6\u8def\u5f84\u6b63\u786e<\/strong>\u3001<strong>\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f<\/strong>\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86pandas\u5e93\u548copenpyxl\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas openpyxl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7136\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5bfc\u5165\u4e2d\u6587\u540d\u5b57\u7684Excel\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u4f7f\u7528 pandas \u8bfb\u53d6 Excel \u6587\u4ef6<\/strong><\/h2>\n<p>file_path = &quot;\u8def\u5f84\/\u5230\/\u4f60\u7684\/\u4e2d\u6587\u540d\u5b57\u6587\u4ef6.xlsx&quot;<\/p>\n<p>df = pd.read_excel(file_path, engine=&#39;openpyxl&#39;)<\/p>\n<h2><strong>\u6253\u5370\u6570\u636e\u6846\u4ee5\u786e\u8ba4\u6570\u636e\u5df2\u6b63\u786e\u5bfc\u5165<\/strong><\/h2>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u786e\u4fdd\u6587\u4ef6\u8def\u5f84\u6b63\u786e<\/strong>\u662f\u5173\u952e\uff0c\u56e0\u4e3a\u5982\u679c\u8def\u5f84\u4e0d\u6b63\u786e\uff0c\u7a0b\u5e8f\u5c06\u65e0\u6cd5\u627e\u5230\u6587\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u6216\u76f8\u5bf9\u8def\u5f84\uff0c\u4f46\u8981\u786e\u4fdd\u8def\u5f84\u4e2d\u5305\u542b\u7684\u4e2d\u6587\u5b57\u7b26\u80fd\u591f\u88ab\u6b63\u786e\u89e3\u6790\u3002\u5bf9\u4e8eWindows\u7cfb\u7edf\uff0c\u5efa\u8bae\u4f7f\u7528\u53cc\u53cd\u659c\u6760\u6216\u524d\u9762\u52a0\u4e0a\u4e00\u4e2a<code>r<\/code>\u6765\u8868\u793a\u539f\u59cb\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = r&quot;C:\\\u8def\u5f84\\\u5230\\\u4f60\u7684\\\u4e2d\u6587\u540d\u5b57\u6587\u4ef6.xlsx&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f<\/strong>\u540c\u6837\u91cd\u8981\u3002\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cpandas\u4f1a\u81ea\u52a8\u68c0\u6d4b\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u4f46\u5982\u679c\u51fa\u73b0\u7f16\u7801\u9519\u8bef\uff0c\u53ef\u4ee5\u624b\u52a8\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u77e5\u9053\u6587\u4ef6\u662f\u4f7f\u7528UTF-8\u7f16\u7801\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.read_excel(file_path, engine=&#39;openpyxl&#39;, encoding=&#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u8981\u5728Python\u4e2d\u5bfc\u5165Excel\u6587\u4ef6\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5pandas\u548copenpyxl\u5e93\u3002\u8fd9\u4e24\u4e2a\u5e93\u5206\u522b\u7528\u4e8e\u6570\u636e\u5904\u7406\u548cExcel\u6587\u4ef6\u7684\u8bfb\u53d6\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas openpyxl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>pandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u800copenpyxl\u5e93\u5219\u7528\u4e8e\u5904\u7406Excel\u6587\u4ef6\u7684\u8bfb\u53d6\u548c\u5199\u5165\u3002\u8fd9\u4e24\u4e2a\u5e93\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5bfc\u5165\u548c\u5904\u7406Excel\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u8bfb\u53d6\u4e2d\u6587\u540d\u5b57\u7684Excel\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5bfc\u5165\u4e2d\u6587\u540d\u5b57\u7684Excel\u6587\u4ef6\u65f6\uff0c\u9996\u5148\u9700\u8981\u786e\u4fdd\u6587\u4ef6\u8def\u5f84\u6b63\u786e\u3002\u53ef\u4ee5\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u6216\u76f8\u5bf9\u8def\u5f84\uff0c\u4f46\u8981\u786e\u4fdd\u8def\u5f84\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26\u80fd\u591f\u88ab\u6b63\u786e\u89e3\u6790\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>file_path = &quot;\u8def\u5f84\/\u5230\/\u4f60\u7684\/\u4e2d\u6587\u540d\u5b57\u6587\u4ef6.xlsx&quot;<\/p>\n<p>df = pd.read_excel(file_path, engine=&#39;openpyxl&#39;)<\/p>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>file_path<\/code>\u53d8\u91cf\u5305\u542b\u4e86Excel\u6587\u4ef6\u7684\u8def\u5f84\u3002<code>pd.read_excel<\/code>\u51fd\u6570\u7528\u4e8e\u8bfb\u53d6Excel\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5185\u5bb9\u5b58\u50a8\u5728\u4e00\u4e2aDataFrame\u5bf9\u8c61\u4e2d\u3002\u6700\u540e\uff0c\u4f7f\u7528<code>print(df.head())<\/code>\u51fd\u6570\u67e5\u770b\u5bfc\u5165\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u5904\u7406\u6587\u4ef6\u8def\u5f84\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u5728Windows\u7cfb\u7edf\u4e2d\uff0c\u5982\u679c\u6587\u4ef6\u8def\u5f84\u4e2d\u5305\u542b\u4e2d\u6587\u5b57\u7b26\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8def\u5f84\u89e3\u6790\u9519\u8bef\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u53cc\u53cd\u659c\u6760\u6216\u524d\u9762\u52a0\u4e0a\u4e00\u4e2a<code>r<\/code>\u6765\u8868\u793a\u539f\u59cb\u5b57\u7b26\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_path = r&quot;C:\\\u8def\u5f84\\\u5230\\\u4f60\u7684\\\u4e2d\u6587\u540d\u5b57\u6587\u4ef6.xlsx&quot;<\/p>\n<p>df = pd.read_excel(file_path, engine=&#39;openpyxl&#39;)<\/p>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u8def\u5f84\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26\u88ab\u6b63\u786e\u89e3\u6790\uff0c\u4ece\u800c\u907f\u514d\u6587\u4ef6\u627e\u4e0d\u5230\u7684\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cpandas\u4f1a\u81ea\u52a8\u68c0\u6d4b\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u4f46\u6709\u65f6\u53ef\u80fd\u4f1a\u51fa\u73b0\u7f16\u7801\u9519\u8bef\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u624b\u52a8\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u77e5\u9053\u6587\u4ef6\u662f\u4f7f\u7528UTF-8\u7f16\u7801\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">df = pd.read_excel(file_path, engine=&#39;openpyxl&#39;, encoding=&#39;utf-8&#39;)<\/p>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26\u88ab\u6b63\u786e\u89e3\u6790\uff0c\u4ece\u800c\u907f\u514d\u7f16\u7801\u9519\u8bef\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u6570\u636e\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26<\/h3>\n<\/p>\n<p><p>\u5728\u5bfc\u5165Excel\u6587\u4ef6\u540e\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002\u5982\u679c\u6570\u636e\u4e2d\u5305\u542b\u4e2d\u6587\u5b57\u7b26\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u95ee\u9898\u3002\u4f8b\u5982\uff0c\u6570\u636e\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26\u53ef\u80fd\u4f1a\u88ab\u9519\u8bef\u5730\u89e3\u6790\u4e3a\u5176\u4ed6\u5b57\u7b26\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u4e2d\u7684\u4e00\u4e9b\u51fd\u6570\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6 Excel \u6587\u4ef6<\/p>\n<p>df = pd.read_excel(file_path, engine=&#39;openpyxl&#39;)<\/p>\n<h2><strong>\u67e5\u770b\u6570\u636e\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26<\/strong><\/h2>\n<p>print(df.head())<\/p>\n<h2><strong>\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406<\/strong><\/h2>\n<p>df[&#39;\u5217\u540d&#39;] = df[&#39;\u5217\u540d&#39;].apply(lambda x: x.strip() if isinstance(x, str) else x)<\/p>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>apply<\/code>\u51fd\u6570\u7528\u4e8e\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002<code>strip<\/code>\u51fd\u6570\u7528\u4e8e\u53bb\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u683c\uff0c\u4ece\u800c\u786e\u4fdd\u6570\u636e\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26\u88ab\u6b63\u786e\u89e3\u6790\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4fdd\u5b58\u5904\u7406\u540e\u7684\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u5728\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u540e\uff0c\u53ef\u80fd\u9700\u8981\u5c06\u5904\u7406\u540e\u7684\u6570\u636e\u4fdd\u5b58\u5230\u4e00\u4e2a\u65b0\u7684Excel\u6587\u4ef6\u4e2d\u3002\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u4e2d\u7684<code>to_excel<\/code>\u51fd\u6570\u5c06\u6570\u636e\u4fdd\u5b58\u5230\u4e00\u4e2a\u65b0\u7684Excel\u6587\u4ef6\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6 Excel \u6587\u4ef6<\/p>\n<p>df = pd.read_excel(file_path, engine=&#39;openpyxl&#39;)<\/p>\n<h2><strong>\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406<\/strong><\/h2>\n<p>df[&#39;\u5217\u540d&#39;] = df[&#39;\u5217\u540d&#39;].apply(lambda x: x.strip() if isinstance(x, str) else x)<\/p>\n<h2><strong>\u4fdd\u5b58\u5904\u7406\u540e\u7684\u6570\u636e\u5230\u4e00\u4e2a\u65b0\u7684 Excel \u6587\u4ef6<\/strong><\/h2>\n<p>new_file_path = &quot;\u8def\u5f84\/\u5230\/\u4f60\u7684\/\u5904\u7406\u540e\u6587\u4ef6.xlsx&quot;<\/p>\n<p>df.to_excel(new_file_path, index=False, engine=&#39;openpyxl&#39;)<\/p>\n<p>print(f&quot;\u5904\u7406\u540e\u7684\u6570\u636e\u5df2\u4fdd\u5b58\u5230 {new_file_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>to_excel<\/code>\u51fd\u6570\u7528\u4e8e\u5c06\u5904\u7406\u540e\u7684\u6570\u636e\u4fdd\u5b58\u5230\u4e00\u4e2a\u65b0\u7684Excel\u6587\u4ef6\u4e2d\u3002\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u8def\u5f84\u548c\u6587\u4ef6\u540d\uff0c\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u88ab\u6b63\u786e\u4fdd\u5b58\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5904\u7406Excel\u6587\u4ef6\u4e2d\u7684\u591a\u5f20\u5de5\u4f5c\u8868<\/h3>\n<\/p>\n<p><p>\u5728\u4e00\u4e9b\u60c5\u51b5\u4e0b\uff0cExcel\u6587\u4ef6\u4e2d\u53ef\u80fd\u5305\u542b\u591a\u5f20\u5de5\u4f5c\u8868\u3002\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u4e2d\u7684<code>sheet_name<\/code>\u53c2\u6570\u6307\u5b9a\u8981\u8bfb\u53d6\u7684\u5de5\u4f5c\u8868\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6 Excel \u6587\u4ef6\u4e2d\u7684\u6307\u5b9a\u5de5\u4f5c\u8868<\/p>\n<p>df = pd.read_excel(file_path, sheet_name=&#39;\u5de5\u4f5c\u8868\u540d\u79f0&#39;, engine=&#39;openpyxl&#39;)<\/p>\n<p>print(df.head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>sheet_name<\/code>\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u8981\u8bfb\u53d6\u7684\u5de5\u4f5c\u8868\u540d\u79f0\u3002\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u5de5\u4f5c\u8868\u7684\u540d\u79f0\uff0c\u9009\u62e9\u8981\u8bfb\u53d6\u7684\u5de5\u4f5c\u8868\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u8bfb\u53d6\u591a\u4e2a\u5de5\u4f5c\u8868<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u8bfb\u53d6Excel\u6587\u4ef6\u4e2d\u7684\u591a\u4e2a\u5de5\u4f5c\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sheet_name<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>None<\/code>\uff0c\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5de5\u4f5c\u8868\u7684\u5b57\u5178\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6 Excel \u6587\u4ef6\u4e2d\u7684\u6240\u6709\u5de5\u4f5c\u8868<\/p>\n<p>dfs = pd.read_excel(file_path, sheet_name=None, engine=&#39;openpyxl&#39;)<\/p>\n<h2><strong>\u67e5\u770b\u6240\u6709\u5de5\u4f5c\u8868\u7684\u540d\u79f0<\/strong><\/h2>\n<p>print(dfs.keys())<\/p>\n<h2><strong>\u67e5\u770b\u6307\u5b9a\u5de5\u4f5c\u8868\u7684\u6570\u636e<\/strong><\/h2>\n<p>print(dfs[&#39;\u5de5\u4f5c\u8868\u540d\u79f0&#39;].head())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>sheet_name<\/code>\u53c2\u6570\u8bbe\u7f6e\u4e3a<code>None<\/code>\uff0c<code>pd.read_excel<\/code>\u51fd\u6570\u5c06\u8fd4\u56de\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u952e\u662f\u5de5\u4f5c\u8868\u7684\u540d\u79f0\uff0c\u503c\u662f\u5bf9\u5e94\u7684DataFrame\u5bf9\u8c61\u3002\u53ef\u4ee5\u901a\u8fc7\u8bbf\u95ee\u5b57\u5178\u4e2d\u7684\u952e\u6765\u67e5\u770b\u6307\u5b9a\u5de5\u4f5c\u8868\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u5904\u7406\u5927\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5305\u542b\u5927\u91cf\u6570\u636e\u7684Excel\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5185\u5b58\u4e0d\u8db3\u7684\u95ee\u9898\u3002\u53ef\u4ee5\u4f7f\u7528<code>chunksize<\/code>\u53c2\u6570\u5c06\u6587\u4ef6\u5206\u5757\u8bfb\u53d6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5206\u5757\u8bfb\u53d6 Excel \u6587\u4ef6<\/p>\n<p>chunksize = 10000<\/p>\n<p>chunks = pd.read_excel(file_path, chunksize=chunksize, engine=&#39;openpyxl&#39;)<\/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\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>chunksize<\/code>\u53c2\u6570\u7528\u4e8e\u6307\u5b9a\u6bcf\u6b21\u8bfb\u53d6\u7684\u6570\u636e\u5757\u5927\u5c0f\u3002\u901a\u8fc7\u5206\u5757\u8bfb\u53d6\uff0c\u53ef\u4ee5\u907f\u514d\u5185\u5b58\u4e0d\u8db3\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5bfc\u5165\u4e2d\u6587\u540d\u5b57\u7684Excel\u6587\u4ef6\u5e76\u4e0d\u590d\u6742\uff0c\u4f46\u9700\u8981\u6ce8\u610f\u4e00\u4e9b\u7ec6\u8282\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5b89\u88c5\u4e86\u5fc5\u8981\u7684\u5e93\uff08pandas\u548copenpyxl\uff09\u3002\u5176\u6b21\uff0c\u786e\u4fdd\u6587\u4ef6\u8def\u5f84\u6b63\u786e\uff0c\u5e76\u6b63\u786e\u5904\u7406\u8def\u5f84\u4e2d\u7684\u4e2d\u6587\u5b57\u7b26\u3002\u6700\u540e\uff0c\u624b\u52a8\u6307\u5b9a\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u4ee5\u786e\u4fdd\u4e2d\u6587\u5b57\u7b26\u88ab\u6b63\u786e\u89e3\u6790\u3002\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5bfc\u5165\u548c\u5904\u7406\u4e2d\u6587\u540d\u5b57\u7684Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u4e2d\u7684\u4e00\u4e9b\u51fd\u6570\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\uff0c\u5e76\u5c06\u5904\u7406\u540e\u7684\u6570\u636e\u4fdd\u5b58\u5230\u4e00\u4e2a\u65b0\u7684Excel\u6587\u4ef6\u4e2d\u3002\u5982\u679cExcel\u6587\u4ef6\u4e2d\u5305\u542b\u591a\u5f20\u5de5\u4f5c\u8868\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sheet_name<\/code>\u53c2\u6570\u6307\u5b9a\u8981\u8bfb\u53d6\u7684\u5de5\u4f5c\u8868\uff0c\u6216\u8005\u8bbe\u7f6e\u4e3a<code>None<\/code>\u4ee5\u8bfb\u53d6\u6240\u6709\u5de5\u4f5c\u8868\u3002\u5bf9\u4e8e\u5305\u542b\u5927\u91cf\u6570\u636e\u7684Excel\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>chunksize<\/code>\u53c2\u6570\u5206\u5757\u8bfb\u53d6\uff0c\u4ee5\u907f\u514d\u5185\u5b58\u4e0d\u8db3\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u80fd\u5e2e\u52a9\u4f60\u5728Python\u4e2d\u987a\u5229\u5bfc\u5165\u4e2d\u6587\u540d\u5b57\u7684Excel\u6587\u4ef6\uff0c\u5e76\u5bf9\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002\u5982\u679c\u6709\u4efb\u4f55\u95ee\u9898\u6216\u9700\u8981\u8fdb\u4e00\u6b65\u7684\u5e2e\u52a9\uff0c\u8bf7\u968f\u65f6\u8054\u7cfb\u6211\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u7684Excel\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u7684Excel\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u6765\u8f7b\u677e\u5bfc\u5165\u6570\u636e\u3002\u786e\u4fdd\u4f7f\u7528<code>encoding=&#39;utf-8&#39;<\/code>\u53c2\u6570\u4ee5\u652f\u6301\u4e2d\u6587\u5b57\u7b26\u3002\u6b64\u5916\uff0c\u5b89\u88c5<code>openpyxl<\/code>\u6216<code>xlrd<\/code>\u5e93\u6765\u8bfb\u53d6Excel\u6587\u4ef6\u4e5f\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\uff0c\u8fd9\u53d6\u51b3\u4e8e\u6587\u4ef6\u7684\u683c\u5f0f\uff08.xlsx\u6216.xls\uff09\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>pd.read_excel(&#39;\u6587\u4ef6\u540d.xlsx&#39;, encoding=&#39;utf-8&#39;)<\/code>\u53ef\u4ee5\u987a\u5229\u8bfb\u53d6\u5305\u542b\u4e2d\u6587\u5b57\u7b26\u7684Excel\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5728\u5bfc\u5165Excel\u65f6\uff0c\u5982\u4f55\u5904\u7406\u4e2d\u6587\u7f16\u7801\u95ee\u9898\uff1f<\/strong><br \/>\u4e2d\u6587\u7f16\u7801\u95ee\u9898\u901a\u5e38\u4f1a\u5bfc\u81f4\u8bfb\u53d6\u5931\u8d25\u6216\u4e71\u7801\u3002\u89e3\u51b3\u65b9\u6848\u662f\u786e\u4fddExcel\u6587\u4ef6\u4f7f\u7528UTF-8\u7f16\u7801\u4fdd\u5b58\uff0c\u5e76\u4f7f\u7528<code>pandas<\/code>\u5e93\u7684<code>read_excel<\/code>\u51fd\u6570\u65f6\u6307\u5b9a\u6b63\u786e\u7684\u5f15\u64ce\uff0c\u5982<code>openpyxl<\/code>\u3002\u5982\u679c\u51fa\u73b0\u4e71\u7801\uff0c\u53ef\u4ee5\u5c1d\u8bd5\u5c06\u6587\u4ef6\u53e6\u5b58\u4e3aCSV\u683c\u5f0f\uff0c\u5e76\u5728\u8bfb\u53d6\u65f6\u6307\u5b9a<code>encoding=&#39;utf-8&#39;<\/code>\uff0c\u786e\u4fdd\u7f16\u7801\u517c\u5bb9\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5bfc\u5165\u4e2d\u6587Excel\u65f6\uff0c\u6709\u54ea\u4e9b\u5e38\u89c1\u9519\u8bef\u53ca\u89e3\u51b3\u65b9\u6cd5\uff1f<\/strong><br \/>\u5728\u5bfc\u5165\u4e2d\u6587Excel\u6587\u4ef6\u65f6\uff0c\u5e38\u89c1\u7684\u9519\u8bef\u5305\u62ec\u6587\u4ef6\u8def\u5f84\u9519\u8bef\u3001\u7f3a\u5c11\u5fc5\u8981\u7684\u5e93\u6216\u4e0d\u517c\u5bb9\u7684Excel\u683c\u5f0f\u3002\u786e\u4fdd\u6587\u4ef6\u8def\u5f84\u6b63\u786e\u4e14\u6587\u4ef6\u5b58\u5728\uff0c\u5b89\u88c5\u6240\u9700\u7684\u5e93\uff08\u5982<code>pandas<\/code>\u548c<code>openpyxl<\/code>\uff09\uff0c\u5e76\u786e\u8ba4Excel\u6587\u4ef6\u683c\u5f0f\u4e0e\u8bfb\u53d6\u5de5\u5177\u517c\u5bb9\u3002\u5982\u679c\u51fa\u73b0\u8bfb\u53d6\u9519\u8bef\uff0c\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u635f\u574f\u6216\u662f\u5426\u5305\u542b\u4e0d\u652f\u6301\u7684\u683c\u5f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u5bfc\u5165\u4e2d\u6587\u540d\u5b57\u7684Excel\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u3002\u3001\u786e\u4fdd\u6587\u4ef6\u8def\u5f84\u6b63\u786e\u3001\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f [&hellip;]","protected":false},"author":3,"featured_media":1116686,"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\/1116684"}],"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=1116684"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1116684\/revisions"}],"predecessor-version":[{"id":1116690,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1116684\/revisions\/1116690"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1116686"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1116684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1116684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1116684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}