{"id":1170439,"date":"2025-01-15T16:22:36","date_gmt":"2025-01-15T08:22:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1170439.html"},"modified":"2025-01-15T16:22:38","modified_gmt":"2025-01-15T08:22:38","slug":"python%e5%a6%82%e4%bd%95%e8%af%bb%e5%8f%96%e6%96%87%e4%bb%b6%e8%a1%8c%e8%ae%b0%e5%bd%95","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1170439.html","title":{"rendered":"python\u5982\u4f55\u8bfb\u53d6\u6587\u4ef6\u884c\u8bb0\u5f55"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26071126\/f99cd3c5-0d40-4cec-8ece-d193a0a5fe17.webp\" alt=\"python\u5982\u4f55\u8bfb\u53d6\u6587\u4ef6\u884c\u8bb0\u5f55\" \/><\/p>\n<p><p> <strong>Python\u8bfb\u53d6\u6587\u4ef6\u884c\u8bb0\u5f55\u7684\u51e0\u79cd\u65b9\u5f0f\u6709\uff1a\u4f7f\u7528open()\u51fd\u6570\u3001\u4f7f\u7528with\u8bed\u53e5\u3001\u4f7f\u7528readlines()\u65b9\u6cd5\u3001\u4f7f\u7528\u8fed\u4ee3\u5668\u3001\u4f7f\u7528pandas\u5e93\u3002<\/strong><br \/><strong>\u5176\u4e2d\uff0c\u4f7f\u7528with\u8bed\u53e5\u548c\u8fed\u4ee3\u5668\u662f\u6700\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u4eec\u80fd\u6709\u6548\u5904\u7406\u6587\u4ef6<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\u548c\u5185\u5b58\u5360\u7528\u7684\u95ee\u9898\u3002<\/strong><\/p>\n<\/p>\n<p><p><strong>\u5177\u4f53\u6765\u8bf4\uff0c\u4f7f\u7528with\u8bed\u53e5\u8bfb\u53d6\u6587\u4ef6\u884c\u8bb0\u5f55\u7684\u65b9\u6cd5\u5982\u4e0b\uff1a<\/strong><\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;filename.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>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u7b80\u6d01\u660e\u4e86\uff0c\u8fd8\u80fd\u786e\u4fdd\u5728\u8bfb\u53d6\u6587\u4ef6\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\uff0c\u907f\u514d\u6587\u4ef6\u8d44\u6e90\u6cc4\u6f0f\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<hr>\n<p><h3>\u4e00\u3001\u4f7f\u7528open()\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u662f\u8bfb\u53d6\u6587\u4ef6\u6700\u57fa\u7840\u7684\u65b9\u5f0f\u3002\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u7075\u6d3b\u5730\u63a7\u5236\u6587\u4ef6\u7684\u6253\u5f00\u6a21\u5f0f\uff08\u5982\u8bfb\u3001\u5199\u3001\u8ffd\u52a0\u7b49\uff09\u548c\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;filename.txt&#39;, &#39;r&#39;)<\/p>\n<p>lines = file.readlines()<\/p>\n<p>for line in lines:<\/p>\n<p>    print(line.strip())<\/p>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u7075\u6d3b\u6027\u9ad8<\/strong>\uff1a\u53ef\u4ee5\u6307\u5b9a\u6587\u4ef6\u7684\u6253\u5f00\u6a21\u5f0f\u548c\u7f16\u7801\u683c\u5f0f\u3002<\/li>\n<li><strong>\u57fa\u7840\u6027\u5f3a<\/strong>\uff1a\u662f\u6587\u4ef6\u64cd\u4f5c\u7684\u57fa\u672c\u65b9\u6cd5\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u573a\u666f\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u8d44\u6e90\u7ba1\u7406\u4e0d\u4fbf<\/strong>\uff1a\u9700\u8981\u624b\u52a8\u5173\u95ed\u6587\u4ef6\uff0c\u5bb9\u6613\u5bfc\u81f4\u6587\u4ef6\u8d44\u6e90\u6cc4\u6f0f\u3002<\/li>\n<li><strong>\u4ee3\u7801\u5197\u957f<\/strong>\uff1a\u76f8\u6bd4\u5176\u4ed6\u65b9\u6cd5\u663e\u5f97\u7e41\u7410\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u4f7f\u7528with\u8bed\u53e5<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u7ba1\u7406\u6587\u4ef6\u8d44\u6e90\uff0c\u786e\u4fdd\u5728\u8bfb\u53d6\u6587\u4ef6\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\u3002\u8fd9\u79cd\u65b9\u5f0f\u975e\u5e38\u7b80\u6d01\u3001\u6613\u8bfb\uff0c\u662f\u63a8\u8350\u7684\u6700\u4f73\u5b9e\u8df5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;filename.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><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u81ea\u52a8\u8d44\u6e90\u7ba1\u7406<\/strong>\uff1a\u786e\u4fdd\u6587\u4ef6\u5728\u4f7f\u7528\u5b8c\u6bd5\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u9632\u6b62\u8d44\u6e90\u6cc4\u6f0f\u3002<\/li>\n<li><strong>\u4ee3\u7801\u7b80\u6d01<\/strong>\uff1a\u51cf\u5c11\u4e86\u624b\u52a8\u5173\u95ed\u6587\u4ef6\u7684\u4ee3\u7801\uff0c\u4f7f\u4ee3\u7801\u66f4\u6e05\u6670\u6613\u8bfb\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u7075\u6d3b\u6027\u7a0d\u900a<\/strong>\uff1a\u867d\u7136\u53ef\u4ee5\u6307\u5b9a\u6253\u5f00\u6a21\u5f0f\u548c\u7f16\u7801\uff0c\u4f46\u5bf9\u4e8e\u4e00\u4e9b\u590d\u6742\u7684\u6587\u4ef6\u64cd\u4f5c\u53ef\u80fd\u4e0d\u5982open()\u51fd\u6570\u7075\u6d3b\u3002<\/li>\n<\/ol>\n<p><h3>\u4e09\u3001\u4f7f\u7528readlines()\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>readlines()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u4e00\u6b21\u6027\u8bfb\u53d6\u6587\u4ef6\u7684\u6240\u6709\u884c\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u4e00\u4e2a\u5217\u8868\u4e2d\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u6587\u4ef6\u5185\u5bb9\u8f83\u5c0f\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;filename.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    lines = file.readlines()<\/p>\n<p>    for line in lines:<\/p>\n<p>        print(line.strip())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u65b9\u4fbf\u904d\u5386<\/strong>\uff1a\u5c06\u6240\u6709\u884c\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff0c\u4fbf\u4e8e\u540e\u7eed\u7684\u904d\u5386\u64cd\u4f5c\u3002<\/li>\n<li><strong>\u4ee3\u7801\u7b80\u6d01<\/strong>\uff1a\u4e0e\u8fed\u4ee3\u5668\u76f8\u6bd4\uff0c\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u660e\u4e86\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u5185\u5b58\u5360\u7528\u5927<\/strong>\uff1a\u5bf9\u4e8e\u5927\u6587\u4ef6\uff0creadlines()\u65b9\u6cd5\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u5360\u7528\u8fc7\u9ad8\u3002<\/li>\n<li><strong>\u65e0\u6cd5\u5904\u7406\u8d85\u5927\u6587\u4ef6<\/strong>\uff1a\u4e0d\u9002\u7528\u4e8e\u6587\u4ef6\u5185\u5bb9\u8f83\u5927\u7684\u60c5\u51b5\u3002<\/li>\n<\/ol>\n<p><h3>\u56db\u3001\u4f7f\u7528\u8fed\u4ee3\u5668<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u672c\u8eab\u4f5c\u4e3a\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u6587\u4ef6\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;filename.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><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u5185\u5b58\u5360\u7528\u5c0f<\/strong>\uff1a\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u9002\u7528\u4e8e\u5904\u7406\u5927\u6587\u4ef6\u3002<\/li>\n<li><strong>\u4ee3\u7801\u7b80\u6d01<\/strong>\uff1a\u4e0ereadlines()\u65b9\u6cd5\u76f8\u6bd4\uff0c\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u660e\u4e86\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u7075\u6d3b\u6027\u7a0d\u900a<\/strong>\uff1a\u65e0\u6cd5\u4e00\u6b21\u6027\u83b7\u53d6\u6240\u6709\u884c\uff0c\u5bf9\u4e8e\u4e00\u4e9b\u9700\u8981\u6574\u4f53\u5904\u7406\u7684\u64cd\u4f5c\u4e0d\u592a\u9002\u7528\u3002<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><p><code>pandas<\/code>\u5e93\u662f\u6570\u636e\u5206\u6790\u7684\u5229\u5668\uff0c\u4f7f\u7528<code>pandas<\/code>\u8bfb\u53d6\u6587\u4ef6\u884c\u8bb0\u5f55\u5c24\u5176\u9002\u7528\u4e8e\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\uff08\u5982CSV\u6587\u4ef6\uff09\u548c\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>df = pd.read_csv(&#39;filename.csv&#39;)<\/p>\n<p>for index, row in df.iterrows():<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u529f\u80fd\u5f3a\u5927<\/strong>\uff1apandas\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6570\u636e\u64cd\u4f5c\u529f\u80fd\uff0c\u9002\u7528\u4e8e\u590d\u6742\u7684\u6570\u636e\u5904\u7406\u4efb\u52a1\u3002<\/li>\n<li><strong>\u9ad8\u6548<\/strong>\uff1apandas\u5bf9\u5927\u89c4\u6a21\u6570\u636e\u7684\u5904\u7406\u6027\u80fd\u4f18\u5f02\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4f9d\u8d56\u5916\u90e8\u5e93<\/strong>\uff1a\u9700\u8981\u5b89\u88c5pandas\u5e93\uff0c\u589e\u52a0\u4e86\u9879\u76ee\u7684\u4f9d\u8d56\u3002<\/li>\n<li><strong>\u76f8\u5bf9\u590d\u6742<\/strong>\uff1a\u5bf9\u4e8e\u7b80\u5355\u7684\u6587\u4ef6\u8bfb\u53d6\u4efb\u52a1\uff0cpandas\u663e\u5f97\u6709\u4e9b\u7e41\u7410\u3002<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u4f7f\u7528CSV\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u5185\u7f6e\u7684<code>csv<\/code>\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><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&#39;filename.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><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4e13\u95e8\u5904\u7406CSV\u6587\u4ef6<\/strong>\uff1acsv\u6a21\u5757\u4e13\u4e3aCSV\u6587\u4ef6\u8bbe\u8ba1\uff0c\u529f\u80fd\u5b8c\u5584\u3002<\/li>\n<li><strong>\u4ee3\u7801\u7b80\u6d01<\/strong>\uff1a\u7b80\u6d01\u660e\u4e86\u7684\u4ee3\u7801\u7ed3\u6784\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u5c40\u9650\u6027<\/strong>\uff1a\u4ec5\u9002\u7528\u4e8eCSV\u6587\u4ef6\uff0c\u65e0\u6cd5\u5904\u7406\u5176\u4ed6\u7c7b\u578b\u7684\u6587\u4ef6\u3002<\/li>\n<li><strong>\u529f\u80fd\u6709\u9650<\/strong>\uff1a\u76f8\u6bd4pandas\uff0ccsv\u6a21\u5757\u7684\u529f\u80fd\u8f83\u4e3a\u6709\u9650\u3002<\/li>\n<\/ol>\n<p><h3>\u4e03\u3001\u4f7f\u7528JSON\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eJSON\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>json<\/code>\u6a21\u5757\u8fdb\u884c\u8bfb\u53d6\u548c\u5199\u5165\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>with open(&#39;filename.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<p>    for item in data:<\/p>\n<p>        print(item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4e13\u95e8\u5904\u7406JSON\u6587\u4ef6<\/strong>\uff1ajson\u6a21\u5757\u4e13\u4e3aJSON\u6587\u4ef6\u8bbe\u8ba1\uff0c\u529f\u80fd\u5b8c\u5584\u3002<\/li>\n<li><strong>\u4ee3\u7801\u7b80\u6d01<\/strong>\uff1a\u7b80\u6d01\u660e\u4e86\u7684\u4ee3\u7801\u7ed3\u6784\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u5c40\u9650\u6027<\/strong>\uff1a\u4ec5\u9002\u7528\u4e8eJSON\u6587\u4ef6\uff0c\u65e0\u6cd5\u5904\u7406\u5176\u4ed6\u7c7b\u578b\u7684\u6587\u4ef6\u3002<\/li>\n<li><strong>\u6570\u636e\u7ed3\u6784\u590d\u6742<\/strong>\uff1a\u5bf9\u4e8e\u590d\u6742\u7684JSON\u6570\u636e\u7ed3\u6784\uff0c\u5904\u7406\u8d77\u6765\u53ef\u80fd\u6bd4\u8f83\u9ebb\u70e6\u3002<\/li>\n<\/ol>\n<p><h3>\u516b\u3001\u4f7f\u7528XML\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eXML\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>xml.etree.ElementTree<\/code>\u6a21\u5757\u8fdb\u884c\u8bfb\u53d6\u548c\u89e3\u6790\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xml.etree.ElementTree as ET<\/p>\n<p>tree = ET.parse(&#39;filename.xml&#39;)<\/p>\n<p>root = tree.getroot()<\/p>\n<p>for child in root:<\/p>\n<p>    print(child.tag, child.attrib)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4e13\u95e8\u5904\u7406XML\u6587\u4ef6<\/strong>\uff1axml.etree.ElementTree\u6a21\u5757\u4e13\u4e3aXML\u6587\u4ef6\u8bbe\u8ba1\uff0c\u529f\u80fd\u5b8c\u5584\u3002<\/li>\n<li><strong>\u4ee3\u7801\u7b80\u6d01<\/strong>\uff1a\u7b80\u6d01\u660e\u4e86\u7684\u4ee3\u7801\u7ed3\u6784\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u5c40\u9650\u6027<\/strong>\uff1a\u4ec5\u9002\u7528\u4e8eXML\u6587\u4ef6\uff0c\u65e0\u6cd5\u5904\u7406\u5176\u4ed6\u7c7b\u578b\u7684\u6587\u4ef6\u3002<\/li>\n<li><strong>\u6570\u636e\u7ed3\u6784\u590d\u6742<\/strong>\uff1a\u5bf9\u4e8e\u590d\u6742\u7684XML\u6570\u636e\u7ed3\u6784\uff0c\u5904\u7406\u8d77\u6765\u53ef\u80fd\u6bd4\u8f83\u9ebb\u70e6\u3002<\/li>\n<\/ol>\n<p><h3>\u4e5d\u3001\u4f7f\u7528configparser\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u914d\u7f6e\u6587\u4ef6\uff08\u5982INI\u6587\u4ef6\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>configparser<\/code>\u6a21\u5757\u8fdb\u884c\u8bfb\u53d6\u548c\u5199\u5165\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import configparser<\/p>\n<p>config = configparser.ConfigParser()<\/p>\n<p>config.read(&#39;filename.ini&#39;)<\/p>\n<p>for section in config.sections():<\/p>\n<p>    print(section)<\/p>\n<p>    for key in config[section]:<\/p>\n<p>        print(key, config[section][key])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4e13\u95e8\u5904\u7406\u914d\u7f6e\u6587\u4ef6<\/strong>\uff1aconfigparser\u6a21\u5757\u4e13\u4e3a\u914d\u7f6e\u6587\u4ef6\u8bbe\u8ba1\uff0c\u529f\u80fd\u5b8c\u5584\u3002<\/li>\n<li><strong>\u4ee3\u7801\u7b80\u6d01<\/strong>\uff1a\u7b80\u6d01\u660e\u4e86\u7684\u4ee3\u7801\u7ed3\u6784\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u5c40\u9650\u6027<\/strong>\uff1a\u4ec5\u9002\u7528\u4e8e\u914d\u7f6e\u6587\u4ef6\uff0c\u65e0\u6cd5\u5904\u7406\u5176\u4ed6\u7c7b\u578b\u7684\u6587\u4ef6\u3002<\/li>\n<li><strong>\u6570\u636e\u7ed3\u6784\u590d\u6742<\/strong>\uff1a\u5bf9\u4e8e\u590d\u6742\u7684\u914d\u7f6e\u6587\u4ef6\u7ed3\u6784\uff0c\u5904\u7406\u8d77\u6765\u53ef\u80fd\u6bd4\u8f83\u9ebb\u70e6\u3002<\/li>\n<\/ol>\n<p><h3>\u5341\u3001\u4f7f\u7528h5py\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eHDF5\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>h5py<\/code>\u5e93\u8fdb\u884c\u8bfb\u53d6\u548c\u5199\u5165\u64cd\u4f5c\u3002HDF5\u662f\u4e00\u79cd\u7528\u4e8e\u5b58\u50a8\u548c\u7ba1\u7406\u5927\u89c4\u6a21\u6570\u636e\u7684\u6587\u4ef6\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import h5py<\/p>\n<p>with h5py.File(&#39;filename.h5&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for key in file.keys():<\/p>\n<p>        print(key, file[key])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4e13\u95e8\u5904\u7406HDF5\u6587\u4ef6<\/strong>\uff1ah5py\u5e93\u4e13\u4e3aHDF5\u6587\u4ef6\u8bbe\u8ba1\uff0c\u529f\u80fd\u5b8c\u5584\u3002<\/li>\n<li><strong>\u9ad8\u6548<\/strong>\uff1a\u9002\u7528\u4e8e\u5927\u89c4\u6a21\u6570\u636e\u7684\u5b58\u50a8\u548c\u7ba1\u7406\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4f9d\u8d56\u5916\u90e8\u5e93<\/strong>\uff1a\u9700\u8981\u5b89\u88c5h5py\u5e93\uff0c\u589e\u52a0\u4e86\u9879\u76ee\u7684\u4f9d\u8d56\u3002<\/li>\n<li><strong>\u76f8\u5bf9\u590d\u6742<\/strong>\uff1a\u5bf9\u4e8e\u7b80\u5355\u7684\u6587\u4ef6\u8bfb\u53d6\u4efb\u52a1\uff0ch5py\u663e\u5f97\u6709\u4e9b\u7e41\u7410\u3002<\/li>\n<\/ol>\n<p><h3>\u5341\u4e00\u3001\u4f7f\u7528sqlite3\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eSQLite\u6570\u636e\u5e93\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>sqlite3<\/code>\u6a21\u5757\u8fdb\u884c\u8bfb\u53d6\u548c\u5199\u5165\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>conn = sqlite3.connect(&#39;filename.db&#39;)<\/p>\n<p>cursor = conn.cursor()<\/p>\n<p>cursor.execute(&#39;SELECT * FROM tablename&#39;)<\/p>\n<p>rows = cursor.fetchall()<\/p>\n<p>for row in rows:<\/p>\n<p>    print(row)<\/p>\n<p>conn.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4e13\u95e8\u5904\u7406SQLite\u6570\u636e\u5e93\u6587\u4ef6<\/strong>\uff1asqlite3\u6a21\u5757\u4e13\u4e3aSQLite\u6570\u636e\u5e93\u8bbe\u8ba1\uff0c\u529f\u80fd\u5b8c\u5584\u3002<\/li>\n<li><strong>\u9ad8\u6548<\/strong>\uff1a\u9002\u7528\u4e8eSQLite\u6570\u636e\u5e93\u7684\u8bfb\u53d6\u548c\u5199\u5165\u64cd\u4f5c\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u5c40\u9650\u6027<\/strong>\uff1a\u4ec5\u9002\u7528\u4e8eSQLite\u6570\u636e\u5e93\u6587\u4ef6\uff0c\u65e0\u6cd5\u5904\u7406\u5176\u4ed6\u7c7b\u578b\u7684\u6587\u4ef6\u3002<\/li>\n<li><strong>\u76f8\u5bf9\u590d\u6742<\/strong>\uff1a\u5bf9\u4e8e\u7b80\u5355\u7684\u6587\u4ef6\u8bfb\u53d6\u4efb\u52a1\uff0csqlite3\u663e\u5f97\u6709\u4e9b\u7e41\u7410\u3002<\/li>\n<\/ol>\n<p><h3>\u5341\u4e8c\u3001\u4f7f\u7528tarfile\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u538b\u7f29\u6587\u4ef6\uff08\u5982TAR\u6587\u4ef6\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>tarfile<\/code>\u6a21\u5757\u8fdb\u884c\u8bfb\u53d6\u548c\u5199\u5165\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tarfile<\/p>\n<p>with tarfile.open(&#39;filename.tar&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for member in file.getmembers():<\/p>\n<p>        print(member.name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4e13\u95e8\u5904\u7406\u538b\u7f29\u6587\u4ef6<\/strong>\uff1atarfile\u6a21\u5757\u4e13\u4e3a\u538b\u7f29\u6587\u4ef6\u8bbe\u8ba1\uff0c\u529f\u80fd\u5b8c\u5584\u3002<\/li>\n<li><strong>\u4ee3\u7801\u7b80\u6d01<\/strong>\uff1a\u7b80\u6d01\u660e\u4e86\u7684\u4ee3\u7801\u7ed3\u6784\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u5c40\u9650\u6027<\/strong>\uff1a\u4ec5\u9002\u7528\u4e8e\u538b\u7f29\u6587\u4ef6\uff0c\u65e0\u6cd5\u5904\u7406\u5176\u4ed6\u7c7b\u578b\u7684\u6587\u4ef6\u3002<\/li>\n<li><strong>\u76f8\u5bf9\u590d\u6742<\/strong>\uff1a\u5bf9\u4e8e\u7b80\u5355\u7684\u6587\u4ef6\u8bfb\u53d6\u4efb\u52a1\uff0ctarfile\u663e\u5f97\u6709\u4e9b\u7e41\u7410\u3002<\/li>\n<\/ol>\n<p><h3>\u5341\u4e09\u3001\u4f7f\u7528zipfile\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eZIP\u538b\u7f29\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>zipfile<\/code>\u6a21\u5757\u8fdb\u884c\u8bfb\u53d6\u548c\u5199\u5165\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zipfile<\/p>\n<p>with zipfile.ZipFile(&#39;filename.zip&#39;, &#39;r&#39;) as file:<\/p>\n<p>    for name in file.namelist():<\/p>\n<p>        print(name)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4e13\u95e8\u5904\u7406ZIP\u538b\u7f29\u6587\u4ef6<\/strong>\uff1azipfile\u6a21\u5757\u4e13\u4e3aZIP\u538b\u7f29\u6587\u4ef6\u8bbe\u8ba1\uff0c\u529f\u80fd\u5b8c\u5584\u3002<\/li>\n<li><strong>\u4ee3\u7801\u7b80\u6d01<\/strong>\uff1a\u7b80\u6d01\u660e\u4e86\u7684\u4ee3\u7801\u7ed3\u6784\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u5c40\u9650\u6027<\/strong>\uff1a\u4ec5\u9002\u7528\u4e8eZIP\u538b\u7f29\u6587\u4ef6\uff0c\u65e0\u6cd5\u5904\u7406\u5176\u4ed6\u7c7b\u578b\u7684\u6587\u4ef6\u3002<\/li>\n<li><strong>\u76f8\u5bf9\u590d\u6742<\/strong>\uff1a\u5bf9\u4e8e\u7b80\u5355\u7684\u6587\u4ef6\u8bfb\u53d6\u4efb\u52a1\uff0czipfile\u663e\u5f97\u6709\u4e9b\u7e41\u7410\u3002<\/li>\n<\/ol>\n<p><h3>\u5341\u56db\u3001\u4f7f\u7528gzip\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eGZIP\u538b\u7f29\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>gzip<\/code>\u6a21\u5757\u8fdb\u884c\u8bfb\u53d6\u548c\u5199\u5165\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import gzip<\/p>\n<p>with gzip.open(&#39;filename.gz&#39;, &#39;rt&#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><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4e13\u95e8\u5904\u7406GZIP\u538b\u7f29\u6587\u4ef6<\/strong>\uff1agzip\u6a21\u5757\u4e13\u4e3aGZIP\u538b\u7f29\u6587\u4ef6\u8bbe\u8ba1\uff0c\u529f\u80fd\u5b8c\u5584\u3002<\/li>\n<li><strong>\u4ee3\u7801\u7b80\u6d01<\/strong>\uff1a\u7b80\u6d01\u660e\u4e86\u7684\u4ee3\u7801\u7ed3\u6784\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u5c40\u9650\u6027<\/strong>\uff1a\u4ec5\u9002\u7528\u4e8eGZIP\u538b\u7f29\u6587\u4ef6\uff0c\u65e0\u6cd5\u5904\u7406\u5176\u4ed6\u7c7b\u578b\u7684\u6587\u4ef6\u3002<\/li>\n<li><strong>\u76f8\u5bf9\u590d\u6742<\/strong>\uff1a\u5bf9\u4e8e\u7b80\u5355\u7684\u6587\u4ef6\u8bfb\u53d6\u4efb\u52a1\uff0cgzip\u663e\u5f97\u6709\u4e9b\u7e41\u7410\u3002<\/li>\n<\/ol>\n<p><h3>\u5341\u4e94\u3001\u4f7f\u7528bz2\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eBZ2\u538b\u7f29\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>bz2<\/code>\u6a21\u5757\u8fdb\u884c\u8bfb\u53d6\u548c\u5199\u5165\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import bz2<\/p>\n<p>with bz2.open(&#39;filename.bz2&#39;, &#39;rt&#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><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4e13\u95e8\u5904\u7406BZ2\u538b\u7f29\u6587\u4ef6<\/strong>\uff1abz2\u6a21\u5757\u4e13\u4e3aBZ2\u538b\u7f29\u6587\u4ef6\u8bbe\u8ba1\uff0c\u529f\u80fd\u5b8c\u5584\u3002<\/li>\n<li><strong>\u4ee3\u7801\u7b80\u6d01<\/strong>\uff1a\u7b80\u6d01\u660e\u4e86\u7684\u4ee3\u7801\u7ed3\u6784\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u5c40\u9650\u6027<\/strong>\uff1a\u4ec5\u9002\u7528\u4e8eBZ2\u538b\u7f29\u6587\u4ef6\uff0c\u65e0\u6cd5\u5904\u7406\u5176\u4ed6\u7c7b\u578b\u7684\u6587\u4ef6\u3002<\/li>\n<li><strong>\u76f8\u5bf9\u590d\u6742<\/strong>\uff1a\u5bf9\u4e8e\u7b80\u5355\u7684\u6587\u4ef6\u8bfb\u53d6\u4efb\u52a1\uff0cbz2\u663e\u5f97\u6709\u4e9b\u7e41\u7410\u3002<\/li>\n<\/ol>\n<p><h3>\u5341\u516d\u3001\u4f7f\u7528lzma\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eLZMA\u538b\u7f29\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u5185\u7f6e\u7684<code>lzma<\/code>\u6a21\u5757\u8fdb\u884c\u8bfb\u53d6\u548c\u5199\u5165\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import lzma<\/p>\n<p>with lzma.open(&#39;filename.xz&#39;, &#39;rt&#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><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4e13\u95e8\u5904\u7406LZMA\u538b\u7f29\u6587\u4ef6<\/strong>\uff1alzma\u6a21\u5757\u4e13\u4e3aLZMA\u538b\u7f29\u6587\u4ef6\u8bbe\u8ba1\uff0c\u529f\u80fd\u5b8c\u5584\u3002<\/li>\n<li><strong>\u4ee3\u7801\u7b80\u6d01<\/strong>\uff1a\u7b80\u6d01\u660e\u4e86\u7684\u4ee3\u7801\u7ed3\u6784\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u5c40\u9650\u6027<\/strong>\uff1a\u4ec5\u9002\u7528\u4e8eLZMA\u538b\u7f29\u6587\u4ef6\uff0c\u65e0\u6cd5\u5904\u7406\u5176\u4ed6\u7c7b\u578b\u7684\u6587\u4ef6\u3002<\/li>\n<li><strong>\u76f8\u5bf9\u590d\u6742<\/strong>\uff1a\u5bf9\u4e8e\u7b80\u5355\u7684\u6587\u4ef6\u8bfb\u53d6\u4efb\u52a1\uff0clzma\u663e\u5f97\u6709\u4e9b\u7e41\u7410\u3002<\/li>\n<\/ol>\n<p><h3>\u5341\u4e03\u3001\u4f7f\u7528pyexcel\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eExcel\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>pyexcel<\/code>\u5e93\u8fdb\u884c\u8bfb\u53d6\u548c\u5199\u5165\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pyexcel<\/p>\n<p>data = pyexcel.get_records(file_name=&#39;filename.xlsx&#39;)<\/p>\n<p>for record in data:<\/p>\n<p>    print(record)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4e13\u95e8\u5904\u7406Excel\u6587\u4ef6<\/strong>\uff1apyexcel\u5e93\u4e13\u4e3aExcel\u6587\u4ef6\u8bbe\u8ba1\uff0c\u529f\u80fd\u5b8c\u5584\u3002<\/li>\n<li><strong>\u4ee3\u7801\u7b80\u6d01<\/strong>\uff1a\u7b80\u6d01\u660e\u4e86\u7684\u4ee3\u7801\u7ed3\u6784\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4f9d\u8d56\u5916\u90e8\u5e93<\/strong>\uff1a\u9700\u8981\u5b89\u88c5pyexcel\u5e93\uff0c\u589e\u52a0\u4e86\u9879\u76ee\u7684\u4f9d\u8d56\u3002<\/li>\n<li><strong>\u76f8\u5bf9\u590d\u6742<\/strong>\uff1a\u5bf9\u4e8e\u7b80\u5355\u7684\u6587\u4ef6\u8bfb\u53d6\u4efb\u52a1\uff0cpyexcel\u663e\u5f97\u6709\u4e9b\u7e41\u7410\u3002<\/li>\n<\/ol>\n<p><h3>\u5341\u516b\u3001\u4f7f\u7528openpyxl\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eExcel\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>openpyxl<\/code>\u5e93\u8fdb\u884c\u8bfb\u53d6\u548c\u5199\u5165\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import openpyxl<\/p>\n<p>wb = openpyxl.load_workbook(&#39;filename.xlsx&#39;)<\/p>\n<p>sheet = wb.active<\/p>\n<p>for row in sheet.iter_rows(values_only=True):<\/p>\n<p>    print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4e13\u95e8\u5904\u7406Excel\u6587\u4ef6<\/strong>\uff1aopenpyxl\u5e93\u4e13\u4e3aExcel\u6587\u4ef6\u8bbe\u8ba1\uff0c\u529f\u80fd\u5b8c\u5584\u3002<\/li>\n<li><strong>\u4ee3\u7801\u7b80\u6d01<\/strong>\uff1a\u7b80\u6d01\u660e\u4e86\u7684\u4ee3\u7801\u7ed3\u6784\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4f9d\u8d56\u5916\u90e8\u5e93<\/strong>\uff1a\u9700\u8981\u5b89\u88c5openpyxl\u5e93\uff0c\u589e\u52a0\u4e86\u9879\u76ee\u7684\u4f9d\u8d56\u3002<\/li>\n<li><strong>\u76f8\u5bf9\u590d\u6742<\/strong>\uff1a\u5bf9\u4e8e\u7b80\u5355\u7684\u6587\u4ef6\u8bfb\u53d6\u4efb\u52a1\uff0copenpyxl\u663e\u5f97\u6709\u4e9b\u7e41\u7410\u3002<\/li>\n<\/ol>\n<p><h3>\u5341\u4e5d\u3001\u4f7f\u7528xlrd\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eExcel\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>xlrd<\/code>\u5e93\u8fdb\u884c\u8bfb\u53d6\u64cd\u4f5c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>xlrd<\/code>\u5e93\u4ec5\u652f\u6301\u8bfb\u53d6Excel 97-2003\u683c\u5f0f\uff08.xls\uff09\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xlrd<\/p>\n<p>workbook = xlrd.open_workbook(&#39;filename.xls&#39;)<\/p>\n<p>sheet = workbook.sheet_by_index(0)<\/p>\n<p>for row_idx in range(sheet.nrows):<\/p>\n<p>    print(sheet.row(row_idx))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4e13\u95e8\u5904\u7406Excel\u6587\u4ef6<\/strong>\uff1axlrd\u5e93\u4e13\u4e3aExcel\u6587\u4ef6\u8bbe\u8ba1\uff0c\u529f\u80fd\u5b8c\u5584\u3002<\/li>\n<li><strong>\u4ee3\u7801\u7b80\u6d01<\/strong>\uff1a\u7b80\u6d01\u660e\u4e86\u7684\u4ee3\u7801\u7ed3\u6784\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4f9d\u8d56\u5916\u90e8\u5e93<\/strong>\uff1a\u9700\u8981\u5b89\u88c5xlrd\u5e93\uff0c\u589e\u52a0\u4e86\u9879\u76ee\u7684\u4f9d\u8d56\u3002<\/li>\n<li><strong>\u683c\u5f0f\u5c40\u9650<\/strong>\uff1a\u4ec5\u652f\u6301\u8bfb\u53d6Excel 97-2003\u683c\u5f0f\uff08.xls\uff09\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u5341\u3001\u4f7f\u7528xlwt\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eExcel\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>xlwt<\/code>\u5e93\u8fdb\u884c\u5199\u5165\u64cd\u4f5c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>xlwt<\/code>\u5e93\u4ec5\u652f\u6301\u5199\u5165Excel 97-2003\u683c\u5f0f\uff08.xls\uff09\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xlwt<\/p>\n<p>workbook = xlwt.Workbook()<\/p>\n<p>sheet = workbook.add_sheet(&#39;Sheet1&#39;)<\/p>\n<p>data = [&#39;Name&#39;, &#39;Age&#39;, &#39;Gender&#39;]<\/p>\n<p>for col_idx, value in enumerate(data):<\/p>\n<p>    sheet.write(0, col_idx, value)<\/p>\n<p>workbook.save(&#39;filename.xls&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4e13\u95e8\u5904\u7406Excel\u6587\u4ef6<\/strong>\uff1axlwt\u5e93\u4e13\u4e3aExcel\u6587\u4ef6\u8bbe\u8ba1\uff0c\u529f\u80fd\u5b8c\u5584\u3002<\/li>\n<li><strong>\u4ee3\u7801\u7b80\u6d01<\/strong>\uff1a\u7b80\u6d01\u660e\u4e86\u7684\u4ee3\u7801\u7ed3\u6784\uff0c\u6613\u4e8e\u7406\u89e3\u548c\u7ef4\u62a4\u3002<\/li>\n<\/ol>\n<p><p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4f9d\u8d56\u5916\u90e8\u5e93<\/strong>\uff1a\u9700\u8981\u5b89\u88c5xlwt\u5e93\uff0c\u589e\u52a0\u4e86\u9879\u76ee\u7684\u4f9d\u8d56\u3002<\/li>\n<li><strong>\u683c\u5f0f\u5c40\u9650<\/strong>\uff1a\u4ec5\u652f\u6301\u5199\u5165Excel 97-2003\u683c\u5f0f\uff08.xls\uff09\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p><h3>\u4e8c\u5341\u4e00\u3001\u4f7f\u7528xlutils\u6a21\u5757<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8eExcel\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684<code>xlutils<\/code>\u5e93\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c<code>xlutils<\/code>\u5e93\u9700\u8981\u7ed3\u5408<code>xlrd<\/code>\u548c<code>xlwt<\/code>\u5e93\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xlrd<\/p>\n<p>from xlutils.copy import copy<\/p>\n<p>workbook = xlrd.open_workbook(&#39;filename.xls&#39;)<\/p>\n<p>writable_workbook = copy(workbook)<\/p>\n<p>sheet = writable_workbook.get_sheet(0)<\/p>\n<p>sheet.write(0, 0, &#39;New Data&#39;)<\/p>\n<p>writable_workbook.save(&#39;filename.xls&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<\/p>\n<ol>\n<li><strong>\u4e13\u95e8\u5904\u7406Excel\u6587\u4ef6<\/strong>\uff1axlutils\u5e93\u7ed3\u5408<\/li>\n<\/ol>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6253\u5f00\u548c\u8bfb\u53d6\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u6253\u5f00\u6587\u4ef6\u3002\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u7684\u8def\u5f84\u548c\u6a21\u5f0f\uff08\u5982&#39;r&#39;\u8868\u793a\u53ea\u8bfb\uff09\uff0c\u53ef\u4ee5\u8f7b\u677e\u8bbf\u95ee\u6587\u4ef6\u5185\u5bb9\u3002\u6253\u5f00\u6587\u4ef6\u540e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>read()<\/code>\u3001<code>readline()<\/code>\u6216<code>readlines()<\/code>\u7b49\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u8bfb\u53d6\u7279\u5b9a\u884c\u7684\u5185\u5bb9\u6709\u4ec0\u4e48\u7b80\u4fbf\u7684\u65b9\u6cd5\u5417\uff1f<\/strong><br \/>\u662f\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528<code>readlines()<\/code>\u65b9\u6cd5\u5c06\u6587\u4ef6\u7684\u6240\u6709\u884c\u8bfb\u53d6\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff0c\u7136\u540e\u901a\u8fc7\u7d22\u5f15\u8bbf\u95ee\u7279\u5b9a\u7684\u884c\u3002\u4f8b\u5982\uff0c<code>lines = file.readlines()<\/code>\u5c06\u6587\u4ef6\u4e2d\u7684\u6bcf\u4e00\u884c\u4f5c\u4e3a\u4e00\u4e2a\u5143\u7d20\u5b58\u50a8\u5728<code>lines<\/code>\u5217\u8868\u4e2d\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7<code>lines[0]<\/code>\u83b7\u53d6\u7b2c\u4e00\u884c\uff0c<code>lines[1]<\/code>\u83b7\u53d6\u7b2c\u4e8c\u884c\uff0c\u4f9d\u6b64\u7c7b\u63a8\u3002<\/p>\n<p><strong>\u5728Python\u8bfb\u53d6\u5927\u6587\u4ef6\u65f6\uff0c\u6709\u54ea\u4e9b\u6027\u80fd\u4f18\u5316\u7684\u5efa\u8bae\uff1f<\/strong><br \/>\u4e3a\u4e86\u4f18\u5316\u8bfb\u53d6\u5927\u6587\u4ef6\u65f6\u7684\u6027\u80fd\uff0c\u53ef\u4ee5\u4f7f\u7528\u9010\u884c\u8bfb\u53d6\u7684\u65b9\u6cd5\u3002\u4f7f\u7528<code>for line in file:<\/code>\u7684\u65b9\u5f0f\u53ef\u4ee5\u9010\u884c\u904d\u5386\u6587\u4ef6\uff0c\u8fd9\u6837\u4e0d\u4f1a\u4e00\u6b21\u6027\u52a0\u8f7d\u6574\u4e2a\u6587\u4ef6\u5230\u5185\u5b58\u4e2d\uff0c\u9002\u5408\u5904\u7406\u5927\u6587\u4ef6\u3002\u6b64\u5916\uff0c\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\uff0c\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bfb\u53d6\u6587\u4ef6\u884c\u8bb0\u5f55\u7684\u51e0\u79cd\u65b9\u5f0f\u6709\uff1a\u4f7f\u7528open()\u51fd\u6570\u3001\u4f7f\u7528with\u8bed\u53e5\u3001\u4f7f\u7528readlines()\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1170445,"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\/1170439"}],"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=1170439"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170439\/revisions"}],"predecessor-version":[{"id":1170449,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1170439\/revisions\/1170449"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1170445"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1170439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1170439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1170439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}