{"id":1043987,"date":"2024-12-31T13:10:48","date_gmt":"2024-12-31T05:10:48","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1043987.html"},"modified":"2024-12-31T13:10:49","modified_gmt":"2024-12-31T05:10:49","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%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1043987.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6\u6587\u4ef6\u6570\u636e\u7c7b\u578b"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/55cc675c-e3a8-42b8-9153-b5f7b25f041a.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6\u6587\u4ef6\u6570\u636e\u7c7b\u578b\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c<strong>\u8bfb\u53d6\u6587\u4ef6\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5<\/strong>\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u6807\u51c6\u5e93\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u3002\u8bfb\u53d6\u6587\u4ef6\u6570\u636e\u65f6\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u4e0d\u540c\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6587\u672c\u6587\u4ef6\u3001\u4e8c\u8fdb\u5236\u6587\u4ef6\u3001CSV\u6587\u4ef6\u3001JSON\u6587\u4ef6\u7b49\u3002\u8be6\u7ec6\u4e86\u89e3\u8fd9\u4e9b\u65b9\u6cd5\u5c06\u4f7f\u4f60\u80fd\u591f\u7075\u6d3b\u5904\u7406\u5404\u79cd\u6587\u4ef6\u7c7b\u578b\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u51e0\u79cd\u5e38\u89c1\u7684\u6587\u4ef6\u8bfb\u53d6\u65b9\u6cd5\uff0c\u5e76\u91cd\u70b9\u8ba8\u8bba\u5176\u4e2d\u4e00\u79cd\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u8bfb\u53d6\u6587\u672c\u6587\u4ef6<\/p>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u3002\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u8def\u5f84\u548c\u6a21\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u6253\u5f00\u548c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>open()<\/code>\u51fd\u6570\u4ee5\u53ea\u8bfb\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>read()<\/code>\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u8bfb\u53d6\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u9732\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u8bfb\u53d6CSV\u6587\u4ef6<\/p>\n<\/p>\n<p><p>CSV\uff08Comma Separated Values\uff09\u6587\u4ef6\u662f\u5e38\u89c1\u7684\u6570\u636e\u5b58\u50a8\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>csv<\/code>\u6a21\u5757\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&#39;example.csv&#39;, &#39;r&#39;) as file:<\/p>\n<p>    reader = csv.reader(file)<\/p>\n<p>    for row in reader:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c<code>csv.reader()<\/code>\u51fd\u6570\u7528\u4e8e\u8bfb\u53d6CSV\u6587\u4ef6\uff0c\u5e76\u5c06\u6bcf\u884c\u6570\u636e\u4f5c\u4e3a\u5217\u8868\u8fd4\u56de\u3002\u53ef\u4ee5\u901a\u8fc7\u904d\u5386<code>reader<\/code>\u5bf9\u8c61\u6765\u8bbf\u95ee\u6bcf\u4e00\u884c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u8bfb\u53d6JSON\u6587\u4ef6<\/p>\n<\/p>\n<p><p>JSON\uff08JavaScript Object Notation\uff09\u6587\u4ef6\u662f\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>with open(&#39;example.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<p>    print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>json.load()<\/code>\u51fd\u6570\u7528\u4e8e\u5c06JSON\u6587\u4ef6\u5185\u5bb9\u89e3\u6790\u4e3aPython\u6570\u636e\u7ed3\u6784\uff08\u5982\u5b57\u5178\u6216\u5217\u8868\uff09\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u8bfb\u53d6\u4e8c\u8fdb\u5236\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e8c\u8fdb\u5236\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u5e76\u6307\u5b9a<code>&#39;rb&#39;<\/code>\u6a21\u5f0f\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.bin&#39;, &#39;rb&#39;) 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\u91cc\uff0c<code>&#39;rb&#39;<\/code>\u6a21\u5f0f\u8868\u793a\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u8bfb\u53d6\u6587\u4ef6\u3002\u8bfb\u53d6\u7684\u5185\u5bb9\u5c06\u662f\u5b57\u8282\u5bf9\u8c61\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u8bfb\u53d6Excel\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u5185\u7f6e\u6a21\u5757\uff0cPython\u8fd8\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7b2c\u4e09\u65b9\u5e93\u6765\u8bfb\u53d6\u7279\u5b9a\u7c7b\u578b\u7684\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u8bfb\u53d6Excel\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.read_excel(&#39;example.xlsx&#39;)<\/p>\n<p>print(df)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>pandas.read_excel()<\/code>\u51fd\u6570\u7528\u4e8e\u8bfb\u53d6Excel\u6587\u4ef6\uff0c\u5e76\u5c06\u5176\u5185\u5bb9\u5b58\u50a8\u4e3a<code>DataFrame<\/code>\u5bf9\u8c61\uff0c\u65b9\u4fbf\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5904\u7406\u5927\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u9010\u884c\u8bfb\u53d6\u53ef\u4ee5\u8282\u7701\u5185\u5b58\uff0c\u907f\u514d\u5360\u7528\u8fc7\u591a\u7cfb\u7edf\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;large_file.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        process(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u91cc\uff0c\u901a\u8fc7\u904d\u5386\u6587\u4ef6\u5bf9\u8c61\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u5e76\u5bf9\u6bcf\u884c\u6570\u636e\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u8bfb\u53d6\u6587\u4ef6\u4e2d\u7684\u7279\u5b9a\u6570\u636e\u7c7b\u578b<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u7279\u5b9a\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982\u6574\u6570\u3001\u6d6e\u70b9\u6570\u6216\u5b57\u7b26\u4e32\u3002\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u6216\u7279\u5b9a\u7684\u89e3\u6790\u65b9\u6cd5\u6765\u63d0\u53d6\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import re<\/p>\n<p>with open(&#39;data.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    numbers = re.findall(r&#39;\\d+&#39;, content)<\/p>\n<p>    print(numbers)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>re.findall()<\/code>\u51fd\u6570\u7528\u4e8e\u5339\u914d\u6587\u4ef6\u5185\u5bb9\u4e2d\u7684\u6240\u6709\u6574\u6570\uff0c\u5e76\u5c06\u5176\u4f5c\u4e3a\u5217\u8868\u8fd4\u56de\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u8bfb\u53d6\u4e0d\u540c\u7c7b\u578b\u7684\u6587\u4ef6\uff0c\u5e76\u6839\u636e\u9700\u8981\u63d0\u53d6\u548c\u5904\u7406\u6570\u636e\u3002<strong>\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93<\/strong>\uff0c\u53ef\u4ee5\u5927\u5927\u7b80\u5316\u6587\u4ef6\u8bfb\u53d6\u64cd\u4f5c\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5c06\u6709\u52a9\u4e8e\u66f4\u9ad8\u6548\u5730\u5904\u7406\u6587\u4ef6\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>Python\u652f\u6301\u54ea\u4e9b\u6587\u4ef6\u6570\u636e\u7c7b\u578b\u7684\u8bfb\u53d6\uff1f<\/strong><br \/>Python\u80fd\u591f\u8bfb\u53d6\u591a\u79cd\u6587\u4ef6\u6570\u636e\u7c7b\u578b\uff0c\u5305\u62ec\u6587\u672c\u6587\u4ef6\uff08\u5982.txt\u548c.csv\uff09\uff0c\u4e8c\u8fdb\u5236\u6587\u4ef6\uff08\u5982.jpg\u548c.mp3\uff09\uff0c\u4ee5\u53ca\u7ed3\u6784\u5316\u6570\u636e\u6587\u4ef6\uff08\u5982.json\u548c.xlsx\uff09\u3002\u4f7f\u7528\u4e0d\u540c\u7684\u5e93\u548c\u65b9\u6cd5\uff0c\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u8bfb\u53d6\u8fd9\u4e9b\u6587\u4ef6\u7c7b\u578b\u3002\u4f8b\u5982\uff0c\u6587\u672c\u6587\u4ef6\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684open()\u51fd\u6570\uff0c\u800cCSV\u6587\u4ef6\u5219\u53ef\u4ee5\u501f\u52a9csv\u6a21\u5757\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u7684\u6700\u5e38\u89c1\u65b9\u6cd5\u662f\u4f7f\u7528open()\u51fd\u6570\u3002\u6253\u5f00\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528read()\u3001readline()\u6216readlines()\u7b49\u65b9\u6cd5\u83b7\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:\n    content = file.read()\n    print(content)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u786e\u4fdd\u6587\u4ef6\u5728\u8bfb\u53d6\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u9632\u6b62\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406CSV\u6587\u4ef6\u7684\u8bfb\u53d6\uff1f<\/strong><br \/>\u5904\u7406CSV\u6587\u4ef6\u7684\u63a8\u8350\u65b9\u5f0f\u662f\u4f7f\u7528csv\u6a21\u5757\u3002\u901a\u8fc7csv.reader\u6216csv.DictReader\u65b9\u6cd5\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u6f14\u793a\u5982\u4f55\u8bfb\u53d6CSV\u6587\u4ef6\uff1a  <\/p>\n<pre><code class=\"language-python\">import csv\n\nwith open(&#39;example.csv&#39;, &#39;r&#39;) as file:\n    reader = csv.reader(file)\n    for row in reader:\n        print(row)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u9010\u884c\u8bbf\u95ee\u6570\u636e\uff0c\u4f7f\u6570\u636e\u5904\u7406\u66f4\u52a0\u7075\u6d3b\u548c\u9ad8\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8bfb\u53d6\u6587\u4ef6\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u3001\u6807\u51c6\u5e93\u4ee5\u53ca\u7b2c\u4e09\u65b9\u5e93\u3002\u8bfb\u53d6\u6587\u4ef6\u6570\u636e\u65f6\uff0c\u4f60\u53ef\u4ee5\u9009 [&hellip;]","protected":false},"author":3,"featured_media":1043990,"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\/1043987"}],"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=1043987"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1043987\/revisions"}],"predecessor-version":[{"id":1043991,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1043987\/revisions\/1043991"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1043990"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1043987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1043987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1043987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}