{"id":1028472,"date":"2024-12-31T10:59:57","date_gmt":"2024-12-31T02:59:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1028472.html"},"modified":"2024-12-31T10:59:59","modified_gmt":"2024-12-31T02:59:59","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96%e6%96%87%e4%bb%b6%e6%95%b0%e6%8d%ae%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1028472.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6\u6587\u4ef6\u6570\u636e\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/fc39273c-79bd-4021-9824-81d6c635199f.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6\u6587\u4ef6\u6570\u636e\u5e93\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u53d6\u6587\u4ef6\u6570\u636e\u5e93\u7684\u65b9\u6cd5<\/strong>\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6eopen()\u51fd\u6570\u3001pandas\u5e93\u3001sqlite3\u5e93\u3001csv\u6a21\u5757\u3002<strong>\u5176\u4e2d\u4f7f\u7528\u5185\u7f6e\u7684open()\u51fd\u6570\u548cpandas\u5e93\u662f\u5e38\u89c1\u7684\u65b9\u5f0f<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528open()\u51fd\u6570<\/p>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684open()\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u8bfb\u53d6\u6587\u4ef6\u6570\u636e\u5e93\u3002\u5b83\u652f\u6301\u591a\u79cd\u6a21\u5f0f\uff0c\u5982\u8bfb\u53d6\uff08&#39;r&#39;\uff09\u3001\u5199\u5165\uff08&#39;w&#39;\uff09\u3001\u8ffd\u52a0\uff08&#39;a&#39;\uff09\u7b49\u3002<\/p>\n<\/p>\n<p><p>1.1 \u57fa\u672c\u7528\u6cd5<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6<\/p>\n<p>file = open(&#39;database.txt&#39;, &#39;r&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>data = file.read()<\/p>\n<h2><strong>\u5173\u95ed\u6587\u4ef6<\/strong><\/h2>\n<p>file.close()<\/p>\n<h2><strong>\u6253\u5370\u8bfb\u53d6\u5230\u7684\u6570\u636e<\/strong><\/h2>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>1.2 \u4f7f\u7528with\u8bed\u53e5<\/p>\n<\/p>\n<p><p>\u4f7f\u7528with\u8bed\u53e5\u53ef\u4ee5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u8d44\u6e90\uff0c\u4e0d\u9700\u8981\u624b\u52a8\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;database.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = file.read()<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>1.3 \u9010\u884c\u8bfb\u53d6<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u6587\u4ef6\u6570\u636e\u91cf\u8f83\u5927\uff0c\u53ef\u4ee5\u9009\u62e9\u9010\u884c\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;database.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        print(line.strip())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528pandas\u5e93<\/p>\n<\/p>\n<p><p>Pandas\u5e93\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5de5\u5177\uff0c\u652f\u6301\u8bfb\u53d6\u591a\u79cd\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u5982csv\u3001excel\u3001sql\u7b49\u3002<\/p>\n<\/p>\n<p><p>2.1 \u8bfb\u53d6CSV\u6587\u4ef6<\/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>data = pd.read_csv(&#39;database.csv&#39;)<\/p>\n<h2><strong>\u6253\u5370\u6570\u636e<\/strong><\/h2>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2.2 \u8bfb\u53d6Excel\u6587\u4ef6<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6Excel\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_excel(&#39;database.xlsx&#39;)<\/p>\n<h2><strong>\u6253\u5370\u6570\u636e<\/strong><\/h2>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>2.3 \u8bfb\u53d6SQL\u6570\u636e\u5e93<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5SQLite\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;database.db&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6570\u636e\u8868<\/strong><\/h2>\n<p>data = pd.read_sql_query(&#39;SELECT * FROM table_name&#39;, conn)<\/p>\n<h2><strong>\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>conn.close()<\/p>\n<h2><strong>\u6253\u5370\u6570\u636e<\/strong><\/h2>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528sqlite3\u5e93<\/p>\n<\/p>\n<p><p>SQLite\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u5d4c\u5165\u5f0f\u6570\u636e\u5e93\uff0cPython\u5185\u7f6e\u4e86sqlite3\u5e93\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u64cd\u4f5cSQLite\u6570\u636e\u5e93\u3002<\/p>\n<\/p>\n<p><p>3.1 \u8fde\u63a5\u6570\u636e\u5e93\u548c\u8bfb\u53d6\u6570\u636e<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5SQLite\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;database.db&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6267\u884c\u67e5\u8be2\u8bed\u53e5<\/strong><\/h2>\n<p>cursor.execute(&#39;SELECT * FROM table_name&#39;)<\/p>\n<h2><strong>\u83b7\u53d6\u6240\u6709\u6570\u636e<\/strong><\/h2>\n<p>data = cursor.fetchall()<\/p>\n<h2><strong>\u5173\u95ed\u6e38\u6807\u548c\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>cursor.close()<\/p>\n<p>conn.close()<\/p>\n<h2><strong>\u6253\u5370\u6570\u636e<\/strong><\/h2>\n<p>for row in data:<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>3.2 \u9010\u884c\u8bfb\u53d6\u6570\u636e<\/p>\n<\/p>\n<p><p>\u5982\u679c\u6570\u636e\u91cf\u8f83\u5927\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fed\u4ee3\u5668\u9010\u884c\u8bfb\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<h2><strong>\u8fde\u63a5SQLite\u6570\u636e\u5e93<\/strong><\/h2>\n<p>conn = sqlite3.connect(&#39;database.db&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u6e38\u6807\u5bf9\u8c61<\/strong><\/h2>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6267\u884c\u67e5\u8be2\u8bed\u53e5<\/strong><\/h2>\n<p>cursor.execute(&#39;SELECT * FROM table_name&#39;)<\/p>\n<h2><strong>\u9010\u884c\u8bfb\u53d6\u6570\u636e<\/strong><\/h2>\n<p>for row in cursor:<\/p>\n<p>    print(row)<\/p>\n<h2><strong>\u5173\u95ed\u6e38\u6807\u548c\u6570\u636e\u5e93\u8fde\u63a5<\/strong><\/h2>\n<p>cursor.close()<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528csv\u6a21\u5757<\/p>\n<\/p>\n<p><p>Python\u7684csv\u6a21\u5757\u4e13\u95e8\u7528\u4e8e\u5904\u7406CSV\u6587\u4ef6\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u548c\u5199\u5165CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>4.1 \u8bfb\u53d6CSV\u6587\u4ef6<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u6253\u5f00CSV\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;database.csv&#39;, &#39;r&#39;) as file:<\/p>\n<p>    reader = csv.reader(file)<\/p>\n<p>    # \u9010\u884c\u8bfb\u53d6<\/p>\n<p>    for row in reader:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>4.2 \u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u8f6c\u6362\u4e3a\u5b57\u5178<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u6253\u5f00CSV\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;database.csv&#39;, &#39;r&#39;) as file:<\/p>\n<p>    reader = csv.DictReader(file)<\/p>\n<p>    # \u9010\u884c\u8bfb\u53d6\u5e76\u8f6c\u6362\u4e3a\u5b57\u5178<\/p>\n<p>    for row in reader:<\/p>\n<p>        print(dict(row))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython\u8bfb\u53d6\u6587\u4ef6\u6570\u636e\u5e93\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c\u5176\u4e2d\u4f7f\u7528\u5185\u7f6e\u7684open()\u51fd\u6570\u548cpandas\u5e93\u662f\u6700\u5e38\u89c1\u7684\u65b9\u5f0f\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5404\u6709\u4f18\u52a3\uff0c\u9009\u62e9\u9002\u5408\u81ea\u5df1\u9700\u6c42\u7684\u65b9\u6cd5\u6700\u4e3a\u91cd\u8981\u3002\u4f7f\u7528open()\u51fd\u6570\u53ef\u4ee5\u7b80\u5355\u5730\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\uff0c\u9002\u5408\u5904\u7406\u8f83\u5c0f\u7684\u6570\u636e\u91cf\uff1b\u4f7f\u7528pandas\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u548c\u5904\u7406\u591a\u79cd\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u9002\u5408\u6570\u636e\u5206\u6790\u548c\u5904\u7406\uff1b\u4f7f\u7528sqlite3\u5e93\u53ef\u4ee5\u65b9\u4fbf\u5730\u64cd\u4f5cSQLite\u6570\u636e\u5e93\uff0c\u9002\u5408\u5b58\u50a8\u548c\u7ba1\u7406\u7ed3\u6784\u5316\u6570\u636e\uff1b\u4f7f\u7528csv\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u548c\u5199\u5165CSV\u6587\u4ef6\uff0c\u9002\u5408\u5904\u7406\u8868\u683c\u6570\u636e\u3002\u5e0c\u671b\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u80fd\u591f\u5e2e\u52a9\u5927\u5bb6\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528Python\u8bfb\u53d6\u6587\u4ef6\u6570\u636e\u5e93\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0c\u8bfb\u53d6\u6587\u4ef6\u6570\u636e\u5e93\u7684\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u8bfb\u53d6\u6587\u4ef6\u6570\u636e\u5e93\u7684\u6b65\u9aa4\u901a\u5e38\u5305\u62ec\u6253\u5f00\u6587\u4ef6\u3001\u89e3\u6790\u6570\u636e\u4ee5\u53ca\u5904\u7406\u6570\u636e\u3002\u9996\u5148\uff0c\u4f7f\u7528Python\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\u3002\u6839\u636e\u6587\u4ef6\u7684\u683c\u5f0f\uff08\u5982CSV\u3001JSON\u3001SQLite\u7b49\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5e94\u7684\u5e93\uff08\u5982<code>csv<\/code>\u3001<code>json<\/code>\u3001<code>sqlite3<\/code>\uff09\u6765\u8bfb\u53d6\u548c\u89e3\u6790\u6570\u636e\u3002\u89e3\u6790\u540e\uff0c\u53ef\u4ee5\u5c06\u6570\u636e\u5b58\u50a8\u5728\u5217\u8868\u3001\u5b57\u5178\u6216\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u4e2d\uff0c\u4ee5\u4fbf\u540e\u7eed\u64cd\u4f5c\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u9002\u5408\u8bfb\u53d6\u4e0d\u540c\u7c7b\u578b\u7684\u6587\u4ef6\u6570\u636e\u5e93\uff1f<\/strong><br \/>\u6839\u636e\u6587\u4ef6\u6570\u636e\u5e93\u7684\u4e0d\u540c\u683c\u5f0f\uff0c\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u5e93\u3002\u4f8b\u5982\uff0c<code>pandas<\/code>\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5e93\uff0c\u9002\u5408\u8bfb\u53d6CSV\u548cExcel\u6587\u4ef6\u3002\u5bf9\u4e8eJSON\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>json<\/code>\u5e93\u3002\u800c\u5982\u679c\u662fSQLite\u6570\u636e\u5e93\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528<code>sqlite3<\/code>\u5e93\u6765\u8fde\u63a5\u548c\u8bfb\u53d6\u6570\u636e\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\uff0c\u4f7f\u5f97\u6570\u636e\u8bfb\u53d6\u548c\u5904\u7406\u53d8\u5f97\u7b80\u5355\u9ad8\u6548\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u8bfb\u53d6\u6587\u4ef6\u6570\u636e\u5e93\u65f6\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5904\u7406\u8bfb\u53d6\u6587\u4ef6\u6570\u636e\u5e93\u65f6\u7684\u9519\u8bef\u53ef\u4ee5\u901a\u8fc7\u5f02\u5e38\u5904\u7406\u6765\u5b9e\u73b0\u3002\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6765\u6355\u6349\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\uff0c\u4f8b\u5982\u6587\u4ef6\u672a\u627e\u5230\u3001\u683c\u5f0f\u9519\u8bef\u7b49\u3002\u5728\u6355\u6349\u5230\u5f02\u5e38\u540e\uff0c\u53ef\u4ee5\u8f93\u51fa\u9519\u8bef\u4fe1\u606f\uff0c\u6216\u8005\u6267\u884c\u5176\u4ed6\u8865\u6551\u63aa\u65bd\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u7a33\u5b9a\u6027\u548c\u7528\u6237\u4f53\u9a8c\u3002\u6b64\u5916\uff0c\u786e\u4fdd\u6587\u4ef6\u8def\u5f84\u6b63\u786e\u548c\u6587\u4ef6\u683c\u5f0f\u7b26\u5408\u9884\u671f\u4e5f\u662f\u907f\u514d\u9519\u8bef\u7684\u5173\u952e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u53d6\u6587\u4ef6\u6570\u636e\u5e93\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u5185\u7f6eopen()\u51fd\u6570\u3001pandas\u5e93\u3001sqlite3\u5e93\u3001csv\u6a21\u5757\u3002 [&hellip;]","protected":false},"author":3,"featured_media":1028479,"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\/1028472"}],"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=1028472"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1028472\/revisions"}],"predecessor-version":[{"id":1028482,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1028472\/revisions\/1028482"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1028479"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1028472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1028472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1028472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}