{"id":1099999,"date":"2025-01-08T15:36:02","date_gmt":"2025-01-08T07:36:02","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1099999.html"},"modified":"2025-01-08T15:36:05","modified_gmt":"2025-01-08T07:36:05","slug":"python%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%9b%9b%e5%88%97%e5%b7%a6%e5%af%b9%e9%bd%90-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1099999.html","title":{"rendered":"python\u5982\u4f55\u5b9e\u73b0\u56db\u5217\u5de6\u5bf9\u9f50"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063428\/d1bab88d-2e2a-405d-94fb-2c3fc315da5b.webp\" alt=\"python\u5982\u4f55\u5b9e\u73b0\u56db\u5217\u5de6\u5bf9\u9f50\" \/><\/p>\n<p><p> <strong>\u901a\u8fc7\u4f7f\u7528Python\u7f16\u7a0b\u8bed\u8a00\uff0c\u53ef\u4ee5\u5b9e\u73b0\u56db\u5217\u5de6\u5bf9\u9f50\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001pandas\u5e93\u3001tabulate\u5e93\u7b49\u3002\u5176\u4e2d\uff0c\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5\u6700\u4e3a\u76f4\u63a5\u548c\u5e38\u7528\uff0c\u672c\u6587\u5c06\u91cd\u70b9\u4ecb\u7ecd\u3002<\/strong>\u901a\u8fc7\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u5217\u7684\u5bbd\u5ea6\u548c\u5bf9\u9f50\u65b9\u5f0f\uff0c\u975e\u5e38\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6570\u636e\u5bf9\u9f50\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5b9e\u73b0\u56db\u5217\u5de6\u5bf9\u9f50<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u5b9e\u73b0\u56db\u5217\u5de6\u5bf9\u9f50\uff0c\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Python\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u529f\u80fd\u3002\u8fd9\u91cc\u4e3b\u8981\u4ecb\u7ecd\u4e09\u79cd\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff1a\u4f7f\u7528\u767e\u5206\u53f7\uff08%\uff09\u3001str.format()\u65b9\u6cd5\u548cf-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u767e\u5206\u53f7\uff08%\uff09\u683c\u5f0f\u5316<\/h3>\n<\/p>\n<p><p>\u767e\u5206\u53f7\u683c\u5f0f\u5316\u662fPython\u4e2d\u6700\u65e9\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u5b83\u7c7b\u4f3c\u4e8eC\u8bed\u8a00\u4e2d\u7684printf\u51fd\u6570\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u5b9e\u73b0\u56db\u5217\u5de6\u5bf9\u9f50\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [<\/p>\n<p>    (&quot;Alice&quot;, &quot;Engineer&quot;, &quot;New York&quot;, &quot;10001&quot;),<\/p>\n<p>    (&quot;Bob&quot;, &quot;Doctor&quot;, &quot;Los Angeles&quot;, &quot;90001&quot;),<\/p>\n<p>    (&quot;Charlie&quot;, &quot;Teacher&quot;, &quot;Chicago&quot;, &quot;60601&quot;),<\/p>\n<p>    (&quot;David&quot;, &quot;Artist&quot;, &quot;San Francisco&quot;, &quot;94101&quot;)<\/p>\n<p>]<\/p>\n<p>for row in data:<\/p>\n<p>    print(&quot;%-10s %-10s %-15s %-10s&quot; % row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>%-10s<\/code>\u8868\u793a\u5de6\u5bf9\u9f50\uff0c\u5bbd\u5ea6\u4e3a10\uff0c<code>%-15s<\/code>\u8868\u793a\u5de6\u5bf9\u9f50\uff0c\u5bbd\u5ea6\u4e3a15\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u636e\u6309\u7167\u6307\u5b9a\u7684\u5bbd\u5ea6\u8fdb\u884c\u5de6\u5bf9\u9f50\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528str.format()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>str.format()\u65b9\u6cd5\u662fPython 2.7\u548c3.0\u4e2d\u5f15\u5165\u7684\u4e00\u79cd\u66f4\u73b0\u4ee3\u548c\u5f3a\u5927\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\u3002\u4e0b\u9762\u662f\u4f7f\u7528str.format()\u65b9\u6cd5\u5b9e\u73b0\u56db\u5217\u5de6\u5bf9\u9f50\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [<\/p>\n<p>    (&quot;Alice&quot;, &quot;Engineer&quot;, &quot;New York&quot;, &quot;10001&quot;),<\/p>\n<p>    (&quot;Bob&quot;, &quot;Doctor&quot;, &quot;Los Angeles&quot;, &quot;90001&quot;),<\/p>\n<p>    (&quot;Charlie&quot;, &quot;Teacher&quot;, &quot;Chicago&quot;, &quot;60601&quot;),<\/p>\n<p>    (&quot;David&quot;, &quot;Artist&quot;, &quot;San Francisco&quot;, &quot;94101&quot;)<\/p>\n<p>]<\/p>\n<p>for row in data:<\/p>\n<p>    print(&quot;{:&lt;10} {:&lt;10} {:&lt;15} {:&lt;10}&quot;.format(*row))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>{:&lt;10}<\/code>\u8868\u793a\u5de6\u5bf9\u9f50\uff0c\u5bbd\u5ea6\u4e3a10\uff0c<code>{:&lt;15}<\/code>\u8868\u793a\u5de6\u5bf9\u9f50\uff0c\u5bbd\u5ea6\u4e3a15\u3002str.format()\u65b9\u6cd5\u63d0\u4f9b\u4e86\u66f4\u76f4\u89c2\u548c\u53ef\u8bfb\u6027\u66f4\u9ad8\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528f-string\uff08\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\uff09<\/h3>\n<\/p>\n<p><p>f-string\u662fPython 3.6\u4e2d\u5f15\u5165\u7684\u4e00\u79cd\u65b0\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\uff0c\u5b83\u66f4\u52a0\u7b80\u6d01\u548c\u9ad8\u6548\u3002\u4e0b\u9762\u662f\u4f7f\u7528f-string\u5b9e\u73b0\u56db\u5217\u5de6\u5bf9\u9f50\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [<\/p>\n<p>    (&quot;Alice&quot;, &quot;Engineer&quot;, &quot;New York&quot;, &quot;10001&quot;),<\/p>\n<p>    (&quot;Bob&quot;, &quot;Doctor&quot;, &quot;Los Angeles&quot;, &quot;90001&quot;),<\/p>\n<p>    (&quot;Charlie&quot;, &quot;Teacher&quot;, &quot;Chicago&quot;, &quot;60601&quot;),<\/p>\n<p>    (&quot;David&quot;, &quot;Artist&quot;, &quot;San Francisco&quot;, &quot;94101&quot;)<\/p>\n<p>]<\/p>\n<p>for row in data:<\/p>\n<p>    name, profession, city, zipcode = row<\/p>\n<p>    print(f&quot;{name:&lt;10} {profession:&lt;10} {city:&lt;15} {zipcode:&lt;10}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>{name:&lt;10}<\/code>\u8868\u793a\u5de6\u5bf9\u9f50\uff0c\u5bbd\u5ea6\u4e3a10\uff0c<code>{city:&lt;15}<\/code>\u8868\u793a\u5de6\u5bf9\u9f50\uff0c\u5bbd\u5ea6\u4e3a15\u3002f-string\u63d0\u4f9b\u4e86\u4e00\u79cd\u66f4\u52a0\u7b80\u6d01\u548c\u9ad8\u6548\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u5f0f\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><p>pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u9002\u7528\u4e8e\u5904\u7406\u548c\u5c55\u793a\u6570\u636e\u3002\u867d\u7136pandas\u5e76\u4e0d\u662f\u4e13\u95e8\u7528\u4e8e\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\uff0c\u4f46\u5b83\u63d0\u4f9b\u4e86\u975e\u5e38\u65b9\u4fbf\u7684\u6570\u636e\u5c55\u793a\u529f\u80fd\u3002\u4e0b\u9762\u662f\u4f7f\u7528pandas\u5e93\u5b9e\u73b0\u56db\u5217\u5de6\u5bf9\u9f50\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>data = [<\/p>\n<p>    (&quot;Alice&quot;, &quot;Engineer&quot;, &quot;New York&quot;, &quot;10001&quot;),<\/p>\n<p>    (&quot;Bob&quot;, &quot;Doctor&quot;, &quot;Los Angeles&quot;, &quot;90001&quot;),<\/p>\n<p>    (&quot;Charlie&quot;, &quot;Teacher&quot;, &quot;Chicago&quot;, &quot;60601&quot;),<\/p>\n<p>    (&quot;David&quot;, &quot;Artist&quot;, &quot;San Francisco&quot;, &quot;94101&quot;)<\/p>\n<p>]<\/p>\n<p>df = pd.DataFrame(data, columns=[&quot;Name&quot;, &quot;Profession&quot;, &quot;City&quot;, &quot;Zipcode&quot;])<\/p>\n<p>print(df.to_string(index=False))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528pandas\u5e93\u521b\u5efa\u4e86\u4e00\u4e2aDataFrame\uff0c\u5e76\u901a\u8fc7<code>to_string<\/code>\u65b9\u6cd5\u8f93\u51fa\u6570\u636e\u3002pandas\u4f1a\u81ea\u52a8\u8c03\u6574\u5217\u5bbd\uff0c\u5e76\u9ed8\u8ba4\u5de6\u5bf9\u9f50\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528tabulate\u5e93<\/h3>\n<\/p>\n<p><p>tabulate\u662f\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u7f8e\u89c2\u8868\u683c\u7684Python\u5e93\uff0c\u9002\u7528\u4e8e\u5c55\u793a\u7ed3\u6784\u5316\u6570\u636e\u3002\u4e0b\u9762\u662f\u4f7f\u7528tabulate\u5e93\u5b9e\u73b0\u56db\u5217\u5de6\u5bf9\u9f50\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from tabulate import tabulate<\/p>\n<p>data = [<\/p>\n<p>    (&quot;Alice&quot;, &quot;Engineer&quot;, &quot;New York&quot;, &quot;10001&quot;),<\/p>\n<p>    (&quot;Bob&quot;, &quot;Doctor&quot;, &quot;Los Angeles&quot;, &quot;90001&quot;),<\/p>\n<p>    (&quot;Charlie&quot;, &quot;Teacher&quot;, &quot;Chicago&quot;, &quot;60601&quot;),<\/p>\n<p>    (&quot;David&quot;, &quot;Artist&quot;, &quot;San Francisco&quot;, &quot;94101&quot;)<\/p>\n<p>]<\/p>\n<p>headers = [&quot;Name&quot;, &quot;Profession&quot;, &quot;City&quot;, &quot;Zipcode&quot;]<\/p>\n<p>print(tabulate(data, headers, tablefmt=&quot;pl<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n&quot;, stralign=&quot;left&quot;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528tabulate\u5e93\u521b\u5efa\u4e86\u4e00\u4e2a\u7f8e\u89c2\u7684\u8868\u683c\uff0c\u5e76\u901a\u8fc7<code>stralign=&quot;left&quot;<\/code>\u53c2\u6570\u5b9e\u73b0\u6570\u636e\u5de6\u5bf9\u9f50\u3002tabulate\u5e93\u63d0\u4f9b\u4e86\u591a\u79cd\u8868\u683c\u6837\u5f0f\u548c\u5bf9\u9f50\u65b9\u5f0f\uff0c\u975e\u5e38\u7075\u6d3b\u548c\u6613\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528PrettyTable\u5e93<\/h3>\n<\/p>\n<p><p>PrettyTable\u662f\u53e6\u4e00\u4e2a\u7528\u4e8e\u521b\u5efa\u7f8e\u89c2\u8868\u683c\u7684Python\u5e93\uff0c\u9002\u7528\u4e8e\u5c55\u793a\u7ed3\u6784\u5316\u6570\u636e\u3002\u4e0b\u9762\u662f\u4f7f\u7528PrettyTable\u5e93\u5b9e\u73b0\u56db\u5217\u5de6\u5bf9\u9f50\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from prettytable import PrettyTable<\/p>\n<p>data = [<\/p>\n<p>    (&quot;Alice&quot;, &quot;Engineer&quot;, &quot;New York&quot;, &quot;10001&quot;),<\/p>\n<p>    (&quot;Bob&quot;, &quot;Doctor&quot;, &quot;Los Angeles&quot;, &quot;90001&quot;),<\/p>\n<p>    (&quot;Charlie&quot;, &quot;Teacher&quot;, &quot;Chicago&quot;, &quot;60601&quot;),<\/p>\n<p>    (&quot;David&quot;, &quot;Artist&quot;, &quot;San Francisco&quot;, &quot;94101&quot;)<\/p>\n<p>]<\/p>\n<p>table = PrettyTable()<\/p>\n<p>table.field_names = [&quot;Name&quot;, &quot;Profession&quot;, &quot;City&quot;, &quot;Zipcode&quot;]<\/p>\n<p>for row in data:<\/p>\n<p>    table.add_row(row)<\/p>\n<p>table.align = &quot;l&quot;<\/p>\n<p>print(table)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528PrettyTable\u5e93\u521b\u5efa\u4e86\u4e00\u4e2a\u7f8e\u89c2\u7684\u8868\u683c\uff0c\u5e76\u901a\u8fc7<code>align<\/code>\u5c5e\u6027\u5b9e\u73b0\u6570\u636e\u5de6\u5bf9\u9f50\u3002PrettyTable\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u8868\u683c\u6837\u5f0f\u548c\u5bf9\u9f50\u65b9\u5f0f\uff0c\u975e\u5e38\u9002\u5408\u5c55\u793a\u7ed3\u6784\u5316\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528Texttable\u5e93<\/h3>\n<\/p>\n<p><p>Texttable\u662f\u4e00\u4e2a\u7528\u4e8e\u5728\u7ec8\u7aef\u4e2d\u521b\u5efa\u8868\u683c\u7684Python\u5e93\uff0c\u9002\u7528\u4e8e\u5c55\u793a\u7ed3\u6784\u5316\u6570\u636e\u3002\u4e0b\u9762\u662f\u4f7f\u7528Texttable\u5e93\u5b9e\u73b0\u56db\u5217\u5de6\u5bf9\u9f50\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from texttable import Texttable<\/p>\n<p>data = [<\/p>\n<p>    (&quot;Alice&quot;, &quot;Engineer&quot;, &quot;New York&quot;, &quot;10001&quot;),<\/p>\n<p>    (&quot;Bob&quot;, &quot;Doctor&quot;, &quot;Los Angeles&quot;, &quot;90001&quot;),<\/p>\n<p>    (&quot;Charlie&quot;, &quot;Teacher&quot;, &quot;Chicago&quot;, &quot;60601&quot;),<\/p>\n<p>    (&quot;David&quot;, &quot;Artist&quot;, &quot;San Francisco&quot;, &quot;94101&quot;)<\/p>\n<p>]<\/p>\n<p>table = Texttable()<\/p>\n<p>table.add_rows([[&quot;Name&quot;, &quot;Profession&quot;, &quot;City&quot;, &quot;Zipcode&quot;]] + data)<\/p>\n<p>table.set_cols_align([&quot;l&quot;, &quot;l&quot;, &quot;l&quot;, &quot;l&quot;])<\/p>\n<p>print(table.draw())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528Texttable\u5e93\u521b\u5efa\u4e86\u4e00\u4e2a\u7f8e\u89c2\u7684\u8868\u683c\uff0c\u5e76\u901a\u8fc7<code>set_cols_align<\/code>\u65b9\u6cd5\u5b9e\u73b0\u6570\u636e\u5de6\u5bf9\u9f50\u3002Texttable\u5e93\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u8868\u683c\u521b\u5efa\u548c\u5bf9\u9f50\u65b9\u5f0f\uff0c\u975e\u5e38\u9002\u5408\u5728\u7ec8\u7aef\u4e2d\u5c55\u793a\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528NumPy\u5e93<\/h3>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684Python\u5e93\uff0c\u867d\u7136NumPy\u5e76\u4e0d\u662f\u4e13\u95e8\u7528\u4e8e\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7NumPy\u6570\u7ec4\u5b9e\u73b0\u6570\u636e\u7684\u5de6\u5bf9\u9f50\u3002\u4e0b\u9762\u662f\u4f7f\u7528NumPy\u5e93\u5b9e\u73b0\u56db\u5217\u5de6\u5bf9\u9f50\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>data = np.array([<\/p>\n<p>    [&quot;Alice&quot;, &quot;Engineer&quot;, &quot;New York&quot;, &quot;10001&quot;],<\/p>\n<p>    [&quot;Bob&quot;, &quot;Doctor&quot;, &quot;Los Angeles&quot;, &quot;90001&quot;],<\/p>\n<p>    [&quot;Charlie&quot;, &quot;Teacher&quot;, &quot;Chicago&quot;, &quot;60601&quot;],<\/p>\n<p>    [&quot;David&quot;, &quot;Artist&quot;, &quot;San Francisco&quot;, &quot;94101&quot;]<\/p>\n<p>])<\/p>\n<p>for row in data:<\/p>\n<p>    print(&quot;{:&lt;10} {:&lt;10} {:&lt;15} {:&lt;10}&quot;.format(*row))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528NumPy\u6570\u7ec4\u5b58\u50a8\u6570\u636e\uff0c\u5e76\u901a\u8fc7\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5b9e\u73b0\u6570\u636e\u5de6\u5bf9\u9f50\u3002NumPy\u5e93\u63d0\u4f9b\u4e86\u9ad8\u6548\u7684\u6570\u636e\u5b58\u50a8\u548c\u5904\u7406\u529f\u80fd\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528CSV\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6570\u636e\u53ef\u80fd\u5b58\u50a8\u5728CSV\u6587\u4ef6\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5b9e\u73b0\u6570\u636e\u5de6\u5bf9\u9f50\u3002\u4e0b\u9762\u662f\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u5b9e\u73b0\u56db\u5217\u5de6\u5bf9\u9f50\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&quot;data.csv&quot;, newline=&quot;&quot;) as csvfile:<\/p>\n<p>    reader = csv.reader(csvfile)<\/p>\n<p>    for row in reader:<\/p>\n<p>        print(&quot;{:&lt;10} {:&lt;10} {:&lt;15} {:&lt;10}&quot;.format(*row))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528csv\u6a21\u5757\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u5e76\u901a\u8fc7\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5b9e\u73b0\u6570\u636e\u5de6\u5bf9\u9f50\u3002csv\u6a21\u5757\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684CSV\u6587\u4ef6\u8bfb\u5199\u529f\u80fd\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528SQL\u6570\u636e\u5e93<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u6570\u636e\u53ef\u80fd\u5b58\u50a8\u5728SQL\u6570\u636e\u5e93\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u67e5\u8be2\u6570\u636e\u5e93\u5e76\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5b9e\u73b0\u6570\u636e\u5de6\u5bf9\u9f50\u3002\u4e0b\u9762\u662f\u67e5\u8be2SQL\u6570\u636e\u5e93\u5e76\u5b9e\u73b0\u56db\u5217\u5de6\u5bf9\u9f50\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u521b\u5efa\u8fde\u63a5<\/strong><\/h2>\n<p>conn = sqlite3.connect(&quot;data.db&quot;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u67e5\u8be2\u6570\u636e<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT Name, Profession, City, Zipcode FROM People&quot;)<\/p>\n<p>rows = cursor.fetchall()<\/p>\n<h2><strong>\u6253\u5370\u6570\u636e<\/strong><\/h2>\n<p>for row in rows:<\/p>\n<p>    print(&quot;{:&lt;10} {:&lt;10} {:&lt;15} {:&lt;10}&quot;.format(*row))<\/p>\n<h2><strong>\u5173\u95ed\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u4f7f\u7528sqlite3\u6a21\u5757\u67e5\u8be2SQLite\u6570\u636e\u5e93\uff0c\u5e76\u901a\u8fc7\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5b9e\u73b0\u6570\u636e\u5de6\u5bf9\u9f50\u3002sqlite3\u6a21\u5757\u63d0\u4f9b\u4e86\u7b80\u5355\u6613\u7528\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u529f\u80fd\uff0c\u975e\u5e38\u9002\u5408\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u7ed3\u8bba<\/h3>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u56db\u5217\u5de6\u5bf9\u9f50\u3002<strong>\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u65b9\u6cd5<\/strong>\uff08\u5305\u62ec\u767e\u5206\u53f7\u683c\u5f0f\u5316\u3001str.format()\u65b9\u6cd5\u548cf-string\uff09\u662f\u6700\u4e3a\u76f4\u63a5\u548c\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u7b80\u5355\u7684\u6570\u636e\u5bf9\u9f50\u4efb\u52a1\u3002<strong>pandas\u3001tabulate\u3001PrettyTable\u3001Texttable\u548cNumPy\u5e93<\/strong>\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u8868\u683c\u6837\u5f0f\u548c\u5bf9\u9f50\u65b9\u5f0f\uff0c\u975e\u5e38\u9002\u5408\u5c55\u793a\u7ed3\u6784\u5316\u6570\u636e\u3002<strong>CSV\u6587\u4ef6\u548cSQL\u6570\u636e\u5e93<\/strong>\u65b9\u6cd5\u9002\u7528\u4e8e\u5904\u7406\u5b58\u50a8\u5728\u6587\u4ef6\u548c\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u6570\u636e\u6765\u6e90\uff0c\u53ef\u4ee5\u9009\u62e9\u6700\u9002\u5408\u7684\u65b9\u6cd5\u5b9e\u73b0\u6570\u636e\u5de6\u5bf9\u9f50\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u73b0\u5b57\u7b26\u4e32\u7684\u56db\u5217\u5de6\u5bf9\u9f50\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5982f-string\u3001<code>str.format()<\/code>\u65b9\u6cd5\u6216\u8005<code>%<\/code>\u683c\u5f0f\u5316\u6765\u5b9e\u73b0\u56db\u5217\u5de6\u5bf9\u9f50\u3002\u53ef\u4ee5\u901a\u8fc7\u6307\u5b9a\u5bbd\u5ea6\u5e76\u4f7f\u7528\u586b\u5145\u5b57\u7b26\u6765\u8fbe\u5230\u8fd9\u4e2a\u76ee\u7684\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>f&quot;{value:&lt;10}&quot;<\/code>\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u5de6\u5bf9\u9f50\u523010\u4e2a\u5b57\u7b26\u7684\u5bbd\u5ea6\u3002<\/p>\n<p><strong>\u662f\u5426\u6709\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0\u8868\u683c\u7684\u5de6\u5bf9\u9f50\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u4e2d\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u5e2e\u52a9\u5b9e\u73b0\u8868\u683c\u683c\u5f0f\u5316\uff0c\u4f8b\u5982<code>pandas<\/code>\u548c<code>prettytable<\/code>\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u548c\u683c\u5f0f\u5316\u529f\u80fd\uff0c\u53ef\u4ee5\u8f7b\u677e\u521b\u5efa\u5e26\u6709\u5de6\u5bf9\u9f50\u5217\u7684\u8868\u683c\u3002\u4f7f\u7528<code>pandas<\/code>\u7684<code>DataFrame<\/code>\u5bf9\u8c61\u53ef\u4ee5\u8f7b\u677e\u7ba1\u7406\u6570\u636e\u5e76\u8f93\u51fa\u5de6\u5bf9\u9f50\u7684\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u4e0d\u7b49\u957f\u7684\u5b57\u7b26\u4e32\u4ee5\u786e\u4fdd\u56db\u5217\u5de6\u5bf9\u9f50\uff1f<\/strong><br \/>\u5728\u5904\u7406\u4e0d\u7b49\u957f\u7684\u5b57\u7b26\u4e32\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u7684<code>ljust()<\/code>\u65b9\u6cd5\u6765\u786e\u4fdd\u6bcf\u4e2a\u5b57\u7b26\u4e32\u90fd\u6309\u7167\u6307\u5b9a\u7684\u5bbd\u5ea6\u8fdb\u884c\u5de6\u5bf9\u9f50\u3002\u901a\u8fc7\u5faa\u73af\u904d\u5386\u6bcf\u4e00\u5217\u7684\u6570\u636e\uff0c\u5e94\u7528<code>ljust(width)<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u4fdd\u8bc1\u6bcf\u5217\u90fd\u6574\u9f50\u6392\u5217\uff0c\u5373\u4f7f\u5b57\u7b26\u4e32\u957f\u5ea6\u4e0d\u540c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u901a\u8fc7\u4f7f\u7528Python\u7f16\u7a0b\u8bed\u8a00\uff0c\u53ef\u4ee5\u5b9e\u73b0\u56db\u5217\u5de6\u5bf9\u9f50\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u3001pandas\u5e93\u3001tab [&hellip;]","protected":false},"author":3,"featured_media":1100008,"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\/1099999"}],"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=1099999"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1099999\/revisions"}],"predecessor-version":[{"id":1100011,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1099999\/revisions\/1100011"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1100008"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1099999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1099999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1099999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}