{"id":1148436,"date":"2025-01-13T16:37:10","date_gmt":"2025-01-13T08:37:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1148436.html"},"modified":"2025-01-13T16:37:13","modified_gmt":"2025-01-13T08:37:13","slug":"%e5%86%85%e7%bd%91python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1148436.html","title":{"rendered":"\u5185\u7f51python\u5982\u4f55\u8bfb\u53d6\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25170711\/df4f4080-86bc-41f7-9540-1963de1a52fb.webp\" alt=\"\u5185\u7f51python\u5982\u4f55\u8bfb\u53d6\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u5185\u7f51Python\u8bfb\u53d6\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684open\u51fd\u6570\u3001\u4f7f\u7528pandas\u5e93\u3001\u4f7f\u7528csv\u6a21\u5757\u3001\u4f7f\u7528configparser\u6a21\u5757\u7b49\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528open\u51fd\u6570\u662f\u6700\u57fa\u7840\u4e14\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u8bfb\u53d6\u5404\u79cd\u6587\u672c\u6587\u4ef6\u3002\u4f7f\u7528pandas\u5e93\u5219\u9002\u5408\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\uff0c\u5982csv\u6587\u4ef6\u3002csv\u6a21\u5757\u4e13\u95e8\u7528\u4e8e\u5904\u7406csv\u6587\u4ef6\uff0cconfigparser\u6a21\u5757\u5219\u7528\u4e8e\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u5185\u7f6e\u7684open\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4f7f\u7528\u5185\u7f6e\u7684open\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528\u5185\u7f6e\u7684open\u51fd\u6570\u53ef\u4ee5\u8f7b\u677e\u6253\u5f00\u5e76\u8bfb\u53d6\u6587\u4ef6\u3002open\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u662f\uff1a<code>open(filename, mode)<\/code>, \u5176\u4e2d<code>filename<\/code>\u662f\u6587\u4ef6\u540d\uff0c<code>mode<\/code>\u662f\u6587\u4ef6\u6253\u5f00\u6a21\u5f0f\uff0c\u5982&quot;r&quot;\u8868\u793a\u8bfb\u6a21\u5f0f\uff0c&quot;w&quot;\u8868\u793a\u5199\u6a21\u5f0f\u7b49\u3002\u8bfb\u53d6\u6587\u4ef6\u7684\u5e38\u89c1\u6b65\u9aa4\u5305\u62ec\u6253\u5f00\u6587\u4ef6\u3001\u8bfb\u53d6\u5185\u5bb9\u3001\u5904\u7406\u5185\u5bb9\u4ee5\u53ca\u5173\u95ed\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6<\/p>\n<p>file = open(&quot;example.txt&quot;, &quot;r&quot;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>content = file.read()<\/p>\n<h2><strong>\u5904\u7406\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>print(content)<\/p>\n<h2><strong>\u5173\u95ed\u6587\u4ef6<\/strong><\/h2>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528open\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>1.1 \u8bfb\u53d6\u6587\u672c\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528open\u51fd\u6570\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u9996\u5148\u9700\u8981\u6253\u5f00\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528read\u3001readline\u6216readlines\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u6700\u540e\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6<\/p>\n<p>file = open(&quot;example.txt&quot;, &quot;r&quot;)<\/p>\n<h2><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>content = file.read()<\/p>\n<h2><strong>\u8f93\u51fa\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>print(content)<\/p>\n<h2><strong>\u5173\u95ed\u6587\u4ef6<\/strong><\/h2>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>file.read()<\/code>\u8bfb\u53d6\u4e86\u6574\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u53d8\u91cf<code>content<\/code>\u4e2d\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>print<\/code>\u51fd\u6570\u8f93\u51fa\u6587\u4ef6\u5185\u5bb9\uff0c\u6700\u540e\u4f7f\u7528<code>file.close()<\/code>\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1.2 \u4f7f\u7528with\u8bed\u53e5\u8bfb\u53d6\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>Python\u7684<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u7b80\u5316\u6587\u4ef6\u64cd\u4f5c\uff0c\u5e76\u4e14\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u4f7f\u7528\u540e\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528with\u8bed\u53e5\u6253\u5f00\u6587\u4ef6<\/p>\n<p>with open(&quot;example.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>with open(&quot;example.txt&quot;, &quot;r&quot;) as file:<\/code>\u8bed\u53e5\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u5c06\u6587\u4ef6\u5bf9\u8c61\u8d4b\u503c\u7ed9\u53d8\u91cf<code>file<\/code>\u3002\u5728<code>with<\/code>\u8bed\u53e5\u5757\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u8fdb\u884c\u5904\u7406\u3002\u5f53<code>with<\/code>\u8bed\u53e5\u5757\u7ed3\u675f\u65f6\uff0c\u6587\u4ef6\u4f1a\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528pandas\u5e93\u8bfb\u53d6\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>2.1 \u8bfb\u53d6CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>pandas\u5e93\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u5c24\u5176\u9002\u5408\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\uff0c\u5982CSV\u6587\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528<code>pandas.read_csv<\/code>\u65b9\u6cd5\u8bfb\u53d6CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/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(&quot;example.csv&quot;)<\/p>\n<h2><strong>\u8f93\u51faDataFrame\u5185\u5bb9<\/strong><\/h2>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165pandas\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>pd.read_csv(&quot;example.csv&quot;)<\/code>\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u53d8\u91cf<code>df<\/code>\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>print(df)<\/code>\u8f93\u51faDataFrame\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>2.2 \u8bfb\u53d6Excel\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>pandas\u5e93\u8fd8\u53ef\u4ee5\u8bfb\u53d6Excel\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas.read_excel<\/code>\u65b9\u6cd5\u8bfb\u53d6Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/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>df = pd.read_excel(&quot;example.xlsx&quot;)<\/p>\n<h2><strong>\u8f93\u51faDataFrame\u5185\u5bb9<\/strong><\/h2>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>pd.read_excel(&quot;example.xlsx&quot;)<\/code>\u8bfb\u53d6Excel\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u53d8\u91cf<code>df<\/code>\u4e2d\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>print(df)<\/code>\u8f93\u51faDataFrame\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528csv\u6a21\u5757\u8bfb\u53d6\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>3.1 \u8bfb\u53d6CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684csv\u6a21\u5757\u4e13\u95e8\u7528\u4e8e\u5904\u7406CSV\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>csv.reader<\/code>\u65b9\u6cd5\u8bfb\u53d6CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/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(&quot;example.csv&quot;, &quot;r&quot;) as file:<\/p>\n<p>    reader = csv.reader(file)<\/p>\n<p>    # \u9010\u884c\u8bfb\u53d6CSV\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>    for row in reader:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165csv\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>with open(&quot;example.csv&quot;, &quot;r&quot;) as file:<\/code>\u8bed\u53e5\u6253\u5f00CSV\u6587\u4ef6\uff0c\u5e76\u5c06\u6587\u4ef6\u5bf9\u8c61\u8d4b\u503c\u7ed9\u53d8\u91cf<code>file<\/code>\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>csv.reader(file)<\/code>\u521b\u5efa\u4e00\u4e2aCSV\u8bfb\u53d6\u5668\uff0c\u5e76\u4f7f\u7528<code>for row in reader:<\/code>\u9010\u884c\u8bfb\u53d6CSV\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>3.2 \u8bfb\u53d6\u5e26\u6709\u6807\u9898\u7684CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5982\u679cCSV\u6587\u4ef6\u5305\u542b\u6807\u9898\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>csv.DictReader<\/code>\u65b9\u6cd5\u8bfb\u53d6CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/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(&quot;example.csv&quot;, &quot;r&quot;) as file:<\/p>\n<p>    reader = csv.DictReader(file)<\/p>\n<p>    # \u9010\u884c\u8bfb\u53d6CSV\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>    for row in reader:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>csv.DictReader(file)<\/code>\u521b\u5efa\u4e00\u4e2aCSV\u5b57\u5178\u8bfb\u53d6\u5668\uff0c\u5e76\u4f7f\u7528<code>for row in reader:<\/code>\u9010\u884c\u8bfb\u53d6CSV\u6587\u4ef6\u5185\u5bb9\u3002\u6bcf\u4e00\u884c\u6570\u636e\u90fd\u4f1a\u4ee5\u5b57\u5178\u7684\u5f62\u5f0f\u8fd4\u56de\uff0c\u5176\u4e2d\u952e\u662f\u5217\u6807\u9898\uff0c\u503c\u662f\u5bf9\u5e94\u7684\u5355\u5143\u683c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528configparser\u6a21\u5757\u8bfb\u53d6\u914d\u7f6e\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>4.1 \u8bfb\u53d6INI\u914d\u7f6e\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684configparser\u6a21\u5757\u4e13\u95e8\u7528\u4e8e\u5904\u7406INI\u914d\u7f6e\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>configparser.ConfigParser<\/code>\u65b9\u6cd5\u8bfb\u53d6INI\u914d\u7f6e\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import configparser<\/p>\n<h2><strong>\u521b\u5efa\u914d\u7f6e\u89e3\u6790\u5668<\/strong><\/h2>\n<p>config = configparser.ConfigParser()<\/p>\n<h2><strong>\u8bfb\u53d6INI\u914d\u7f6e\u6587\u4ef6<\/strong><\/h2>\n<p>config.read(&quot;example.ini&quot;)<\/p>\n<h2><strong>\u8f93\u51fa\u914d\u7f6e\u5185\u5bb9<\/strong><\/h2>\n<p>for section in config.sections():<\/p>\n<p>    print(&quot;Section:&quot;, section)<\/p>\n<p>    for key, value in config.items(section):<\/p>\n<p>        print(f&quot;{key} = {value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165configparser\u6a21\u5757\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u914d\u7f6e\u89e3\u6790\u5668<code>config = configparser.ConfigParser()<\/code>\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>config.read(&quot;example.ini&quot;)<\/code>\u8bfb\u53d6INI\u914d\u7f6e\u6587\u4ef6\u3002\u6700\u540e\uff0c\u6211\u4eec\u904d\u5386\u914d\u7f6e\u6587\u4ef6\u7684\u8282\u548c\u952e\u503c\u5bf9\uff0c\u5e76\u8f93\u51fa\u914d\u7f6e\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>4.2 \u8bfb\u53d6\u7279\u5b9a\u7684\u914d\u7f6e\u9879<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>config.get<\/code>\u65b9\u6cd5\u8bfb\u53d6\u7279\u5b9a\u7684\u914d\u7f6e\u9879\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import configparser<\/p>\n<h2><strong>\u521b\u5efa\u914d\u7f6e\u89e3\u6790\u5668<\/strong><\/h2>\n<p>config = configparser.ConfigParser()<\/p>\n<h2><strong>\u8bfb\u53d6INI\u914d\u7f6e\u6587\u4ef6<\/strong><\/h2>\n<p>config.read(&quot;example.ini&quot;)<\/p>\n<h2><strong>\u8bfb\u53d6\u7279\u5b9a\u7684\u914d\u7f6e\u9879<\/strong><\/h2>\n<p>host = config.get(&quot;database&quot;, &quot;host&quot;)<\/p>\n<p>port = config.getint(&quot;database&quot;, &quot;port&quot;)<\/p>\n<p>print(&quot;Host:&quot;, host)<\/p>\n<p>print(&quot;Port:&quot;, port)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>config.get(&quot;database&quot;, &quot;host&quot;)<\/code>\u8bfb\u53d6&quot;database&quot;\u8282\u4e2d\u7684&quot;host&quot;\u914d\u7f6e\u9879\uff0c\u5e76\u4f7f\u7528<code>config.getint(&quot;database&quot;, &quot;port&quot;)<\/code>\u8bfb\u53d6&quot;database&quot;\u8282\u4e2d\u7684&quot;port&quot;\u914d\u7f6e\u9879\u3002\u7136\u540e\uff0c\u6211\u4eec\u8f93\u51fa\u8bfb\u53d6\u5230\u7684\u914d\u7f6e\u9879\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528json\u6a21\u5757\u8bfb\u53d6\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>5.1 \u8bfb\u53d6JSON\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684json\u6a21\u5757\u4e13\u95e8\u7528\u4e8e\u5904\u7406JSON\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json.load<\/code>\u65b9\u6cd5\u8bfb\u53d6JSON\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u6253\u5f00JSON\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&quot;example.json&quot;, &quot;r&quot;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<h2><strong>\u8f93\u51faJSON\u5185\u5bb9<\/strong><\/h2>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165json\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>with open(&quot;example.json&quot;, &quot;r&quot;) as file:<\/code>\u8bed\u53e5\u6253\u5f00JSON\u6587\u4ef6\uff0c\u5e76\u5c06\u6587\u4ef6\u5bf9\u8c61\u8d4b\u503c\u7ed9\u53d8\u91cf<code>file<\/code>\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>json.load(file)<\/code>\u8bfb\u53d6JSON\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u53d8\u91cf<code>data<\/code>\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>print(data)<\/code>\u8f93\u51faJSON\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>5.2 \u8bfb\u53d6\u5e76\u5904\u7406JSON\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u8bfb\u53d6JSON\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u5bf9JSON\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u6253\u5f00JSON\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&quot;example.json&quot;, &quot;r&quot;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<h2><strong>\u5904\u7406JSON\u6570\u636e<\/strong><\/h2>\n<p>for item in data[&quot;items&quot;]:<\/p>\n<p>    print(&quot;Name:&quot;, item[&quot;name&quot;])<\/p>\n<p>    print(&quot;Price:&quot;, item[&quot;price&quot;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u8bfb\u53d6\u4e86\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u9879\u76ee\u7684JSON\u6587\u4ef6\uff0c\u5e76\u904d\u5386<code>data[&quot;items&quot;]<\/code>\u4e2d\u7684\u6bcf\u4e2a\u9879\u76ee\uff0c\u8f93\u51fa\u9879\u76ee\u7684\u540d\u79f0\u548c\u4ef7\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u4f7f\u7528yaml\u6a21\u5757\u8bfb\u53d6\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>6.1 \u8bfb\u53d6YAML\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>YAML\u662f\u4e00\u79cd\u5e38\u7528\u7684\u914d\u7f6e\u6587\u4ef6\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>PyYAML<\/code>\u5e93\u6765\u8bfb\u53d6YAML\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yaml<\/p>\n<h2><strong>\u6253\u5f00YAML\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&quot;example.yaml&quot;, &quot;r&quot;) as file:<\/p>\n<p>    data = yaml.<a href=\"https:\/\/docs.pingcode.com\/agile\/agile-at-scale\/what-is-safe\" target=\"_blank\">SAFe<\/a>_load(file)<\/p>\n<h2><strong>\u8f93\u51faYAML\u5185\u5bb9<\/strong><\/h2>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165yaml\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>with open(&quot;example.yaml&quot;, &quot;r&quot;) as file:<\/code>\u8bed\u53e5\u6253\u5f00YAML\u6587\u4ef6\uff0c\u5e76\u5c06\u6587\u4ef6\u5bf9\u8c61\u8d4b\u503c\u7ed9\u53d8\u91cf<code>file<\/code>\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>yaml.safe_load(file)<\/code>\u8bfb\u53d6YAML\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u53d8\u91cf<code>data<\/code>\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>print(data)<\/code>\u8f93\u51faYAML\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>6.2 \u8bfb\u53d6\u5e76\u5904\u7406YAML\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u8bfb\u53d6YAML\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u5bf9YAML\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yaml<\/p>\n<h2><strong>\u6253\u5f00YAML\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&quot;example.yaml&quot;, &quot;r&quot;) as file:<\/p>\n<p>    data = yaml.safe_load(file)<\/p>\n<h2><strong>\u5904\u7406YAML\u6570\u636e<\/strong><\/h2>\n<p>for item in data[&quot;items&quot;]:<\/p>\n<p>    print(&quot;Name:&quot;, item[&quot;name&quot;])<\/p>\n<p>    print(&quot;Price:&quot;, item[&quot;price&quot;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u8bfb\u53d6\u4e86\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u9879\u76ee\u7684YAML\u6587\u4ef6\uff0c\u5e76\u904d\u5386<code>data[&quot;items&quot;]<\/code>\u4e2d\u7684\u6bcf\u4e2a\u9879\u76ee\uff0c\u8f93\u51fa\u9879\u76ee\u7684\u540d\u79f0\u548c\u4ef7\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528pickle\u6a21\u5757\u8bfb\u53d6\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>7.1 \u8bfb\u53d6Pickle\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684pickle\u6a21\u5757\u7528\u4e8e\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316Python\u5bf9\u8c61\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pickle.load<\/code>\u65b9\u6cd5\u8bfb\u53d6Pickle\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<h2><strong>\u6253\u5f00Pickle\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&quot;example.pkl&quot;, &quot;rb&quot;) as file:<\/p>\n<p>    data = pickle.load(file)<\/p>\n<h2><strong>\u8f93\u51faPickle\u5185\u5bb9<\/strong><\/h2>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165pickle\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>with open(&quot;example.pkl&quot;, &quot;rb&quot;) as file:<\/code>\u8bed\u53e5\u6253\u5f00Pickle\u6587\u4ef6\uff0c\u5e76\u5c06\u6587\u4ef6\u5bf9\u8c61\u8d4b\u503c\u7ed9\u53d8\u91cf<code>file<\/code>\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>pickle.load(file)<\/code>\u8bfb\u53d6Pickle\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u53d8\u91cf<code>data<\/code>\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>print(data)<\/code>\u8f93\u51faPickle\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>7.2 \u8bfb\u53d6\u5e76\u5904\u7406Pickle\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u8bfb\u53d6Pickle\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u5bf9Pickle\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<h2><strong>\u6253\u5f00Pickle\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&quot;example.pkl&quot;, &quot;rb&quot;) as file:<\/p>\n<p>    data = pickle.load(file)<\/p>\n<h2><strong>\u5904\u7406Pickle\u6570\u636e<\/strong><\/h2>\n<p>for item in data:<\/p>\n<p>    print(&quot;Item:&quot;, item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u8bfb\u53d6\u4e86\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u9879\u76ee\u7684Pickle\u6587\u4ef6\uff0c\u5e76\u904d\u5386<code>data<\/code>\u4e2d\u7684\u6bcf\u4e2a\u9879\u76ee\uff0c\u8f93\u51fa\u6bcf\u4e2a\u9879\u76ee\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u516b\u3001\u4f7f\u7528h5py\u6a21\u5757\u8bfb\u53d6\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>8.1 \u8bfb\u53d6HDF5\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>HDF5\u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u50a8\u5927\u89c4\u6a21\u6570\u636e\u7684\u6587\u4ef6\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528<code>h5py<\/code>\u5e93\u6765\u8bfb\u53d6HDF5\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import h5py<\/p>\n<h2><strong>\u6253\u5f00HDF5\u6587\u4ef6<\/strong><\/h2>\n<p>with h5py.File(&quot;example.h5&quot;, &quot;r&quot;) as file:<\/p>\n<p>    data = file[&quot;dataset&quot;][:]<\/p>\n<h2><strong>\u8f93\u51faHDF5\u5185\u5bb9<\/strong><\/h2>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165h5py\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>with h5py.File(&quot;example.h5&quot;, &quot;r&quot;) as file:<\/code>\u8bed\u53e5\u6253\u5f00HDF5\u6587\u4ef6\uff0c\u5e76\u5c06\u6587\u4ef6\u5bf9\u8c61\u8d4b\u503c\u7ed9\u53d8\u91cf<code>file<\/code>\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>file[&quot;dataset&quot;][:]<\/code>\u8bfb\u53d6HDF5\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u96c6\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u53d8\u91cf<code>data<\/code>\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>print(data)<\/code>\u8f93\u51faHDF5\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>8.2 \u8bfb\u53d6\u5e76\u5904\u7406HDF5\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u8bfb\u53d6HDF5\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u5bf9HDF5\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import h5py<\/p>\n<h2><strong>\u6253\u5f00HDF5\u6587\u4ef6<\/strong><\/h2>\n<p>with h5py.File(&quot;example.h5&quot;, &quot;r&quot;) as file:<\/p>\n<p>    data = file[&quot;dataset&quot;][:]<\/p>\n<h2><strong>\u5904\u7406HDF5\u6570\u636e<\/strong><\/h2>\n<p>for item in data:<\/p>\n<p>    print(&quot;Item:&quot;, item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u8bfb\u53d6\u4e86\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u9879\u76ee\u7684HDF5\u6587\u4ef6\uff0c\u5e76\u904d\u5386<code>data<\/code>\u4e2d\u7684\u6bcf\u4e2a\u9879\u76ee\uff0c\u8f93\u51fa\u6bcf\u4e2a\u9879\u76ee\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e5d\u3001\u4f7f\u7528sqlite3\u6a21\u5757\u8bfb\u53d6\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>9.1 \u8bfb\u53d6SQLite\u6570\u636e\u5e93\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684sqlite3\u6a21\u5757\u7528\u4e8e\u5904\u7406SQLite\u6570\u636e\u5e93\uff0c\u53ef\u4ee5\u4f7f\u7528<code>sqlite3.connect<\/code>\u65b9\u6cd5\u8fde\u63a5SQLite\u6570\u636e\u5e93\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/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(&quot;example.db&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6e38\u6807<\/strong><\/h2>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6267\u884c\u67e5\u8be2<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT * FROM table_name&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c<\/strong><\/h2>\n<p>rows = cursor.fetchall()<\/p>\n<h2><strong>\u8f93\u51fa\u67e5\u8be2\u7ed3\u679c<\/strong><\/h2>\n<p>for row in rows:<\/p>\n<p>    print(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\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165sqlite3\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>sqlite3.connect(&quot;example.db&quot;)<\/code>\u8fde\u63a5SQLite\u6570\u636e\u5e93\u6587\u4ef6\uff0c\u5e76\u5c06\u8fde\u63a5\u5bf9\u8c61\u8d4b\u503c\u7ed9\u53d8\u91cf<code>conn<\/code>\u3002\u63a5\u7740\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u6e38\u6807<code>cursor = conn.cursor()<\/code>\uff0c\u5e76\u4f7f\u7528<code>cursor.execute(&quot;SELECT * FROM table_name&quot;)<\/code>\u6267\u884c\u67e5\u8be2\u3002\u7136\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>cursor.fetchall()<\/code>\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c\uff0c\u5e76\u904d\u5386\u8f93\u51fa\u6bcf\u4e00\u884c\u6570\u636e\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>conn.close()<\/code>\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\u3002<\/p>\n<\/p>\n<p><h4>9.2 \u8bfb\u53d6\u5e76\u5904\u7406SQLite\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u8bfb\u53d6SQLite\u6570\u636e\u5e93\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u5bf9SQLite\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/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(&quot;example.db&quot;)<\/p>\n<h2><strong>\u521b\u5efa\u6e38\u6807<\/strong><\/h2>\n<p>cursor = conn.cursor()<\/p>\n<h2><strong>\u6267\u884c\u67e5\u8be2<\/strong><\/h2>\n<p>cursor.execute(&quot;SELECT * FROM table_name&quot;)<\/p>\n<h2><strong>\u83b7\u53d6\u67e5\u8be2\u7ed3\u679c<\/strong><\/h2>\n<p>rows = cursor.fetchall()<\/p>\n<h2><strong>\u5904\u7406SQLite\u6570\u636e<\/strong><\/h2>\n<p>for row in rows:<\/p>\n<p>    print(&quot;ID:&quot;, row[0])<\/p>\n<p>    print(&quot;Name:&quot;, row[1])<\/p>\n<p>    print(&quot;Age:&quot;, row[2])<\/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\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u8bfb\u53d6\u4e86\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u8bb0\u5f55\u7684SQLite\u6570\u636e\u5e93\uff0c\u5e76\u904d\u5386<code>rows<\/code>\u4e2d\u7684\u6bcf\u4e2a\u8bb0\u5f55\uff0c\u8f93\u51fa\u8bb0\u5f55\u7684ID\u3001\u540d\u79f0\u548c\u5e74\u9f84\u3002<\/p>\n<\/p>\n<p><h3>\u5341\u3001\u4f7f\u7528requests\u6a21\u5757\u8bfb\u53d6\u6587\u4ef6<\/h3>\n<\/p>\n<p><h4>10.1 \u8bfb\u53d6\u8fdc\u7a0b\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>Python\u7684requests\u6a21\u5757\u7528\u4e8e\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u53ef\u4ee5\u4f7f\u7528<code>requests.get<\/code>\u65b9\u6cd5\u8bfb\u53d6\u8fdc\u7a0b\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u53d1\u9001HTTP\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(&quot;https:\/\/example.com\/file.txt&quot;)<\/p>\n<h2><strong>\u8bfb\u53d6\u8fdc\u7a0b\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>content = response.text<\/p>\n<h2><strong>\u8f93\u51fa\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5bfc\u5165requests\u6a21\u5757\uff0c\u7136\u540e\u4f7f\u7528<code>requests.get(&quot;https:\/\/example.com\/file.txt&quot;)<\/code>\u53d1\u9001HTTP GET\u8bf7\u6c42\uff0c\u5e76\u5c06\u54cd\u5e94\u5bf9\u8c61\u8d4b\u503c\u7ed9\u53d8\u91cf<code>response<\/code>\u3002\u63a5\u7740\uff0c\u6211\u4eec\u4f7f\u7528<code>response.text<\/code>\u8bfb\u53d6\u8fdc\u7a0b\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u53d8\u91cf<code>content<\/code>\u4e2d\u3002\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528<code>print(content)<\/code>\u8f93\u51fa\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h4>10.2 \u8bfb\u53d6\u5e76\u5904\u7406\u8fdc\u7a0b\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u8bfb\u53d6\u8fdc\u7a0b\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u5bf9\u6587\u4ef6\u5185\u5bb9\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<h2><strong>\u53d1\u9001HTTP\u8bf7\u6c42<\/strong><\/h2>\n<p>response = requests.get(&quot;https:\/\/example.com\/data.json&quot;)<\/p>\n<h2><strong>\u8bfb\u53d6\u8fdc\u7a0b\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p>data = response.json()<\/p>\n<h2><strong>\u5904\u7406JSON\u6570\u636e<\/strong><\/h2>\n<p>for item in data[&quot;items&quot;]:<\/p>\n<p>    print(&quot;Name:&quot;, item[&quot;name&quot;])<\/p>\n<p>    print(&quot;Price:&quot;, item[&quot;price&quot;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u8bfb\u53d6\u4e86\u4e00\u4e2a\u5305\u542b\u591a\u4e2a\u9879\u76ee\u7684\u8fdc\u7a0bJSON\u6587\u4ef6\uff0c\u5e76\u904d\u5386<code>data[&quot;items&quot;]<\/code>\u4e2d\u7684\u6bcf\u4e2a\u9879\u76ee\uff0c\u8f93\u51fa\u9879\u76ee\u7684\u540d\u79f0\u548c\u4ef7\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5728\u5185\u7f51\u73af\u5883\u4e2d\u4f7f\u7528Python\u8bfb\u53d6\u6587\u4ef6\u7684\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528open\u51fd\u6570\u3001pandas\u5e93\u3001csv\u6a21\u5757\u3001configparser\u6a21\u5757\u3001json\u6a21\u5757\u3001yaml\u6a21\u5757\u3001pickle\u6a21\u5757\u3001h5py\u6a21\u5757\u3001sqlite3\u6a21\u5757\u548crequests\u6a21\u5757\u7b49\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u7279\u5b9a\u7684\u5e94\u7528\u573a\u666f\u548c\u4f18\u52bf\uff0c\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u638c\u63e1\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6\u5e76\u5904\u7406\u5404\u79cd\u7c7b\u578b\u7684\u6587\u4ef6\u548c\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728\u5185\u7f51\u73af\u5883\u4e2d\u4f7f\u7528Python\u8bfb\u53d6\u6587\u4ef6\uff1f<\/strong><br \/>\u5728\u5185\u7f51\u73af\u5883\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570\u6765\u8bfb\u53d6\u6587\u4ef6\u3002\u6700\u5e38\u7528\u7684\u65b9\u5f0f\u662f\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u8fd9\u6837\u8bfb\u53d6\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;\u6587\u4ef6\u8def\u5f84\/\u6587\u4ef6\u540d.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:\n    content = file.read()\n    print(content)\n<\/code><\/pre>\n<p>\u786e\u4fdd\u60a8\u63d0\u4f9b\u7684\u6587\u4ef6\u8def\u5f84\u662f\u6b63\u786e\u7684\uff0c\u5e76\u4e14Python\u6709\u6743\u9650\u8bbf\u95ee\u8be5\u6587\u4ef6\u3002<\/p>\n<p><strong>\u5728\u5185\u7f51\u4e2d\u5982\u4f55\u5904\u7406\u4e0d\u540c\u683c\u5f0f\u7684\u6587\u4ef6\uff1f<\/strong><br \/>Python\u53ef\u4ee5\u5904\u7406\u591a\u79cd\u6587\u4ef6\u683c\u5f0f\uff0c\u5305\u62ecCSV\u3001Excel\u548cJSON\u7b49\u3002\u5bf9\u4e8eCSV\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\uff1b\u5bf9\u4e8eExcel\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\uff1b\u800c\u5bf9\u4e8eJSON\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\u3002\u4ee5\u4e0b\u662f\u8bfb\u53d6CSV\u6587\u4ef6\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import csv\n\nwith open(&#39;\u6587\u4ef6\u8def\u5f84\/\u6587\u4ef6\u540d.csv&#39;, mode=&#39;r&#39;, encoding=&#39;utf-8&#39;) as file:\n    reader = csv.reader(file)\n    for row in reader:\n        print(row)\n<\/code><\/pre>\n<p>\u5bf9\u4e8eExcel\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u7684<code>read_excel<\/code>\u51fd\u6570\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u5185\u7f51\u73af\u5883\u4e2d\u5904\u7406\u6587\u4ef6\u8bfb\u53d6\u7684\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u7f16\u5199\u6587\u4ef6\u8bfb\u53d6\u4ee3\u7801\u65f6\uff0c\u6355\u83b7\u5f02\u5e38\u662f\u4e00\u4e2a\u597d\u4e60\u60ef\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u7ed3\u6784\u6765\u5904\u7406\u6587\u4ef6\u672a\u627e\u5230\u6216\u6743\u9650\u4e0d\u8db3\u7b49\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">try:\n    with open(&#39;\u6587\u4ef6\u8def\u5f84\/\u6587\u4ef6\u540d.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file:\n        content = file.read()\n        print(content)\nexcept FileNotFoundError:\n    print(&quot;\u6587\u4ef6\u672a\u627e\u5230\uff0c\u8bf7\u68c0\u67e5\u8def\u5f84\u662f\u5426\u6b63\u786e\u3002&quot;)\nexcept PermissionError:\n    print(&quot;\u6743\u9650\u4e0d\u8db3\uff0c\u65e0\u6cd5\u8bbf\u95ee\u8be5\u6587\u4ef6\u3002&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\uff0c\u5e76\u5e2e\u52a9\u7528\u6237\u66f4\u597d\u5730\u7406\u89e3\u53d1\u751f\u4e86\u4ec0\u4e48\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5185\u7f51Python\u8bfb\u53d6\u6587\u4ef6\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684open\u51fd\u6570\u3001\u4f7f\u7528pandas\u5e93\u3001\u4f7f\u7528csv\u6a21\u5757\u3001\u4f7f\u7528config [&hellip;]","protected":false},"author":3,"featured_media":1148440,"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\/1148436"}],"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=1148436"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1148436\/revisions"}],"predecessor-version":[{"id":1148441,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1148436\/revisions\/1148441"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1148440"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1148436"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1148436"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1148436"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}