{"id":1087616,"date":"2025-01-08T13:36:49","date_gmt":"2025-01-08T05:36:49","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1087616.html"},"modified":"2025-01-08T13:36:53","modified_gmt":"2025-01-08T05:36:53","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e7%94%9f%e6%88%90%e8%a1%a8%e6%a0%bc%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1087616.html","title":{"rendered":"\u5982\u4f55\u7528python\u751f\u6210\u8868\u683c\u6570\u636e\u7c7b\u578b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24200447\/5482242a-3e95-46ea-8dc7-e3a8c852fa9d.webp\" alt=\"\u5982\u4f55\u7528python\u751f\u6210\u8868\u683c\u6570\u636e\u7c7b\u578b\" \/><\/p>\n<p><p> <strong>\u8981\u7528Python\u751f\u6210\u8868\u683c\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u3001openpyxl\u3001csv\u7b49\u5e93\u3002<\/strong> \u9996\u5148\uff0cpandas \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u548c\u64cd\u4f5c\u5e93\uff0copenpyxl \u53ef\u4ee5\u5904\u7406 Excel \u6587\u4ef6\uff0c\u800c csv \u5e93\u5219\u9002\u7528\u4e8e\u5904\u7406 CSV \u6587\u4ef6\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u751f\u6210\u8868\u683c\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528 Pandas \u751f\u6210\u8868\u683c\u6570\u636e<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5 Pandas<\/h4>\n<\/p>\n<p><p>\u8981\u4f7f\u7528 pandas\uff0c\u9996\u5148\u9700\u8981\u5b89\u88c5\u8fd9\u4e2a\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7 pip \u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pandas<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa DataFrame<\/h4>\n<\/p>\n<p><p>pandas \u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u7ed3\u6784\u662f DataFrame\u3002\u53ef\u4ee5\u4ece\u5b57\u5178\u3001\u5217\u8868\u7b49\u591a\u79cd\u6570\u636e\u7ed3\u6784\u521b\u5efa DataFrame\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5b57\u5178<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;John&#39;, &#39;Anna&#39;, &#39;Peter&#39;, &#39;Linda&#39;],<\/p>\n<p>    &#39;Age&#39;: [28, 24, 35, 32],<\/p>\n<p>    &#39;City&#39;: [&#39;New York&#39;, &#39;Paris&#39;, &#39;Berlin&#39;, &#39;London&#39;]<\/p>\n<p>}<\/p>\n<h2><strong>\u5c06\u5b57\u5178\u8f6c\u6362\u4e3a DataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame(data)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u4fdd\u5b58 DataFrame \u5230\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06 DataFrame \u4fdd\u5b58\u4e3a\u591a\u79cd\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u6bd4\u5982 CSV\u3001Excel \u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4fdd\u5b58\u4e3a CSV \u6587\u4ef6<\/p>\n<p>df.to_csv(&#39;data.csv&#39;, index=False)<\/p>\n<h2><strong>\u4fdd\u5b58\u4e3a Excel \u6587\u4ef6<\/strong><\/h2>\n<p>df.to_excel(&#39;data.xlsx&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528 Openpyxl \u751f\u6210 Excel \u8868\u683c<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5 Openpyxl<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u901a\u8fc7 pip \u5b89\u88c5 openpyxl\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install openpyxl<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa Excel \u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 openpyxl \u53ef\u4ee5\u521b\u5efa\u548c\u64cd\u4f5c Excel \u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from openpyxl import Workbook<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a Workbook \u5bf9\u8c61<\/strong><\/h2>\n<p>wb = Workbook()<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u6d3b\u8dc3\u7684\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>ws = wb.active<\/p>\n<h2><strong>\u6dfb\u52a0\u6570\u636e\u5230\u5de5\u4f5c\u8868<\/strong><\/h2>\n<p>ws[&#39;A1&#39;] = &#39;Name&#39;<\/p>\n<p>ws[&#39;B1&#39;] = &#39;Age&#39;<\/p>\n<p>ws[&#39;C1&#39;] = &#39;City&#39;<\/p>\n<p>data = [<\/p>\n<p>    [&#39;John&#39;, 28, &#39;New York&#39;],<\/p>\n<p>    [&#39;Anna&#39;, 24, &#39;Paris&#39;],<\/p>\n<p>    [&#39;Peter&#39;, 35, &#39;Berlin&#39;],<\/p>\n<p>    [&#39;Linda&#39;, 32, &#39;London&#39;]<\/p>\n<p>]<\/p>\n<p>for row in data:<\/p>\n<p>    ws.append(row)<\/p>\n<h2><strong>\u4fdd\u5b58 Excel \u6587\u4ef6<\/strong><\/h2>\n<p>wb.save(&#39;data.xlsx&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528 CSV \u5e93\u751f\u6210 CSV \u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>1\u3001CSV \u5e93\u65e0\u9700\u5b89\u88c5<\/h4>\n<\/p>\n<p><p>Python \u5185\u7f6e\u4e86 csv \u5e93\uff0c\u4e0d\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa CSV \u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528 csv \u5e93\u53ef\u4ee5\u521b\u5efa\u548c\u64cd\u4f5c CSV \u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    [&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;],<\/p>\n<p>    [&#39;John&#39;, 28, &#39;New York&#39;],<\/p>\n<p>    [&#39;Anna&#39;, 24, &#39;Paris&#39;],<\/p>\n<p>    [&#39;Peter&#39;, 35, &#39;Berlin&#39;],<\/p>\n<p>    [&#39;Linda&#39;, 32, &#39;London&#39;]<\/p>\n<p>]<\/p>\n<h2><strong>\u521b\u5efa CSV \u6587\u4ef6\u5e76\u5199\u5165\u6570\u636e<\/strong><\/h2>\n<p>with open(&#39;data.csv&#39;, mode=&#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><h3>\u56db\u3001\u4f7f\u7528 PrettyTable \u751f\u6210\u8868\u683c<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5 PrettyTable<\/h4>\n<\/p>\n<p><p>PrettyTable \u662f\u4e00\u4e2a\u7528\u4e8e\u751f\u6210 ASCII \u8868\u683c\u7684\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7 pip \u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install prettytable<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u548c\u663e\u793a\u8868\u683c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from prettytable import PrettyTable<\/p>\n<h2><strong>\u521b\u5efa PrettyTable \u5bf9\u8c61<\/strong><\/h2>\n<p>table = PrettyTable()<\/p>\n<h2><strong>\u6dfb\u52a0\u5217<\/strong><\/h2>\n<p>table.add_column(&quot;Name&quot;, [&quot;John&quot;, &quot;Anna&quot;, &quot;Peter&quot;, &quot;Linda&quot;])<\/p>\n<p>table.add_column(&quot;Age&quot;, [28, 24, 35, 32])<\/p>\n<p>table.add_column(&quot;City&quot;, [&quot;New York&quot;, &quot;Paris&quot;, &quot;Berlin&quot;, &quot;London&quot;])<\/p>\n<h2><strong>\u6253\u5370\u8868\u683c<\/strong><\/h2>\n<p>print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528 Tabulate \u751f\u6210\u8868\u683c<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5 Tabulate<\/h4>\n<\/p>\n<p><p>Tabulate \u662f\u53e6\u4e00\u4e2a\u751f\u6210 ASCII \u8868\u683c\u7684\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7 pip \u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install tabulate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u548c\u663e\u793a\u8868\u683c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from tabulate import tabulate<\/p>\n<h2><strong>\u6570\u636e<\/strong><\/h2>\n<p>data = [<\/p>\n<p>    [&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;],<\/p>\n<p>    [&#39;John&#39;, 28, &#39;New York&#39;],<\/p>\n<p>    [&#39;Anna&#39;, 24, &#39;Paris&#39;],<\/p>\n<p>    [&#39;Peter&#39;, 35, &#39;Berlin&#39;],<\/p>\n<p>    [&#39;Linda&#39;, 32, &#39;London&#39;]<\/p>\n<p>]<\/p>\n<h2><strong>\u6253\u5370\u8868\u683c<\/strong><\/h2>\n<p>print(tabulate(data, headers=&#39;firstrow&#39;, tablefmt=&#39;grid&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528 Numpy \u751f\u6210\u8868\u683c\u6570\u636e<\/h3>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5 Numpy<\/h4>\n<\/p>\n<p><p>Numpy \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u53ef\u4ee5\u901a\u8fc7 pip \u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install numpy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u521b\u5efa\u548c\u64cd\u4f5c Numpy \u6570\u7ec4<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa Numpy \u6570\u7ec4<\/strong><\/h2>\n<p>data = np.array([<\/p>\n<p>    [&#39;Name&#39;, &#39;Age&#39;, &#39;City&#39;],<\/p>\n<p>    [&#39;John&#39;, 28, &#39;New York&#39;],<\/p>\n<p>    [&#39;Anna&#39;, 24, &#39;Paris&#39;],<\/p>\n<p>    [&#39;Peter&#39;, 35, &#39;Berlin&#39;],<\/p>\n<p>    [&#39;Linda&#39;, 32, &#39;London&#39;]<\/p>\n<p>])<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u4ecb\u7ecd\u4e86\u591a\u79cd\u4f7f\u7528 Python \u751f\u6210\u8868\u683c\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\uff0c\u5305\u62ec pandas\u3001openpyxl\u3001csv\u3001PrettyTable\u3001Tabulate \u548c Numpy\u3002<strong>pandas \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5de5\u5177\uff0c\u9002\u7528\u4e8e\u5927\u591a\u6570\u6570\u636e\u5904\u7406\u9700\u6c42\uff1bopenpyxl \u4e13\u6ce8\u4e8e Excel \u6587\u4ef6\u64cd\u4f5c\uff1bcsv \u5e93\u9002\u7528\u4e8e\u5904\u7406 CSV \u6587\u4ef6\uff1bPrettyTable \u548c Tabulate \u5219\u9002\u7528\u4e8e\u751f\u6210\u7f8e\u89c2\u7684 ASCII \u8868\u683c\uff1bNumpy \u5219\u662f\u79d1\u5b66\u8ba1\u7b97\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177<\/strong>\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5de5\u4f5c\u6548\u7387\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u751f\u6210\u4e0d\u540c\u683c\u5f0f\u7684\u8868\u683c\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u5e93\u751f\u6210\u4e0d\u540c\u683c\u5f0f\u7684\u8868\u683c\u6570\u636e\u3002\u4f8b\u5982\uff0cPandas\u5e93\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u548c\u64cd\u4f5c\u6570\u636e\u6846\uff0c\u800cOpenPyXL\u548cXlsxWriter\u53ef\u4ee5\u7528\u4e8e\u751f\u6210Excel\u6587\u4ef6\u3002\u5982\u679c\u9700\u8981\u751f\u6210HTML\u8868\u683c\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u7684to_html()\u65b9\u6cd5\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u548c\u683c\u5f0f\u53d6\u51b3\u4e8e\u5177\u4f53\u9700\u6c42\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u751f\u6210\u8868\u683c\u65f6\uff0c\u5982\u4f55\u5904\u7406\u7f3a\u5931\u6570\u636e\uff1f<\/strong><br \/>\u5728\u751f\u6210\u8868\u683c\u6570\u636e\u65f6\uff0c\u7f3a\u5931\u6570\u636e\u662f\u4e00\u4e2a\u5e38\u89c1\u95ee\u9898\u3002Pandas\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u5904\u7406\u7f3a\u5931\u503c\uff0c\u5305\u62ec\u586b\u5145\u7f3a\u5931\u503c\u3001\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\u6216\u5217\u7b49\u3002\u4f7f\u7528fillna()\u51fd\u6570\u53ef\u4ee5\u7528\u6307\u5b9a\u7684\u503c\u66ff\u6362\u7f3a\u5931\u503c\uff0c\u800cdropna()\u51fd\u6570\u53ef\u4ee5\u5220\u9664\u5305\u542b\u7f3a\u5931\u503c\u7684\u884c\u6216\u5217\uff0c\u4ee5\u786e\u4fdd\u751f\u6210\u7684\u8868\u683c\u6570\u636e\u5b8c\u6574\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u548c\u5bfc\u51fa\u8868\u683c\u6570\u636e\uff1f<\/strong><br \/>Python\u4e2d\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u751f\u6210\u548c\u5bfc\u51fa\u8868\u683c\u6570\u636e\uff0c\u5e38\u7528\u7684\u5305\u62ecPandas\u3001OpenPyXL\u3001XlsxWriter\u548cTabulate\u3002Pandas\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u5904\u7406\uff0cOpenPyXL\u548cXlsxWriter\u9002\u5408\u751f\u6210Excel\u6587\u4ef6\uff0cTabulate\u5219\u53ef\u4ee5\u751f\u6210\u7f8e\u89c2\u7684\u6587\u672c\u8868\u683c\u3002\u9009\u62e9\u5408\u9002\u7684\u5e93\u53ef\u4ee5\u6839\u636e\u9879\u76ee\u9700\u6c42\u548c\u6570\u636e\u683c\u5f0f\u6765\u51b3\u5b9a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u751f\u6210\u8868\u683c\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u3001openpyxl\u3001csv\u7b49\u5e93\u3002 \u9996\u5148\uff0cpandas \u662f [&hellip;]","protected":false},"author":3,"featured_media":1087622,"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\/1087616"}],"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=1087616"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1087616\/revisions"}],"predecessor-version":[{"id":1087631,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1087616\/revisions\/1087631"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1087622"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1087616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1087616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1087616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}