{"id":205293,"date":"2025-05-29T17:01:54","date_gmt":"2025-05-29T09:01:54","guid":{"rendered":"https:\/\/server.hk\/cnblog\/205293\/"},"modified":"2025-05-29T17:01:54","modified_gmt":"2025-05-29T09:01:54","slug":"%e4%bd%bf%e7%94%a8-pandas-%e5%92%8c-glob-%e5%af%bc%e5%85%a5-excel-%e6%96%87%e4%bb%b6%e6%97%b6%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3excel-%e6%96%87%e4%bb%b6%e6%a0%bc%e5%bc%8f%e6%97%a0%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/205293\/","title":{"rendered":"\u4f7f\u7528 Pandas \u548c glob \u5bfc\u5165 Excel \u6587\u4ef6\u65f6\u5982\u4f55\u89e3\u51b3\u201cExcel \u6587\u4ef6\u683c\u5f0f\u65e0\u6cd5\u786e\u5b9a\u201d\u9519\u8bef\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4f7f\u7528 Pandas \u548c glob \u5bfc\u5165 Excel \u6587\u4ef6\u65f6\u5982\u4f55\u89e3\u51b3\u201cExcel \u6587\u4ef6\u683c\u5f0f\u65e0\u6cd5\u786e\u5b9a\u201d\u9519\u8bef\uff1f<\/h1>\n<p>\u5728\u6587\u7ae0\u5b9e\u6218\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u4e00\u4e9b\u8fd9\u6837\u90a3\u6837\u7684\u95ee\u9898\uff0c\u7136\u540e\u8981\u5361\u597d\u534a\u5929\uff0c\u7b49\u95ee\u9898\u89e3\u51b3\u4e86\u624d\u53d1\u73b0\u539f\u6765\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u662f\u6ca1\u6709\u638c\u63e1\u597d\u3002\u4eca\u5929\u5c31\u6574\u7406\u5206\u4eab\u300a\u4f7f\u7528 Pandas \u548c glob \u5bfc\u5165 Excel \u6587\u4ef6\u65f6\u5982\u4f55\u89e3\u51b3\u201cExcel \u6587\u4ef6\u683c\u5f0f\u65e0\u6cd5\u786e\u5b9a\u201d\u9519\u8bef\uff1f\u300b\uff0c\u804a\u804a\uff0c\u5e0c\u671b\u53ef\u4ee5\u5e2e\u52a9\u5230\u6b63\u5728\u52aa\u529b\u8d5a\u94b1\u7684\u4f60\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241128\/17327675346747ef2e4913e.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u5728\u4f7f\u7528 pandas \u548c glob \u5bfc\u5165 excel \u6587\u4ef6\u65f6\u7684\u4e0d\u5e38\u89c1\u7684\u5f15\u64ce\u6307\u5b9a\u96be\u9898<\/strong><\/p>\n<p>\u901a\u8fc7 pandas \u5e93\u548c glob \u6a21\u5757\u8bfb\u53d6 excel \u6587\u4ef6\u65f6\uff0c\u65e9\u671f\u9636\u6bb5\u53ef\u80fd\u51fa\u73b0\u201cexcel \u6587\u4ef6\u683c\u5f0f\u65e0\u6cd5\u786e\u5b9a\uff0c\u60a8\u5fc5\u987b\u624b\u52a8\u6307\u5b9a\u5f15\u64ce\u201d\u7684\u9519\u8bef\u4fe1\u606f\u3002\u8fd9\u4e2a\u7279\u6b8a\u9519\u8bef\u7684\u6839\u6e90\u5728\u4e8e\u9690\u85cf\u7684\u4e34\u65f6\u6587\u4ef6 ~$filename.xlsx\uff0c\u5f53 ms excel \u6253\u5f00 xlsx \u6587\u4ef6\u65f6\u4f1a\u5728\u540c\u4e00\u76ee\u5f55\u4e2d\u521b\u5efa\u3002<\/p>\n<p>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6709\u4e24\u4e2a\u53ef\u884c\u7684\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<ol>\n<li><strong>\u5173\u95ed\u6253\u5f00\u7684\u6240\u6709 excel \u6587\u4ef6\uff0c\u5e76\u5220\u9664\u6240\u6709\u9690\u85cf\u7684\u4e34\u65f6\u6587\u4ef6 (~$filename.xlsx)<\/strong>\u3002\u968f\u540e\uff0c\u4ee3\u7801\u5c31\u53ef\u4ee5\u6b63\u5e38\u8fd0\u884c\uff0c\u6ca1\u6709\u4efb\u4f55\u5f15\u64ce\u6307\u5b9a\u9519\u8bef\u3002<\/li>\n<li><strong>\u660e\u786e\u6307\u5b9a\u5f15\u64ce\u53c2\u6570<\/strong>\u3002\u5bf9\u4e8e\u5927\u591a\u6570 xlsx \u6587\u4ef6\uff0c<strong>openpyxl<\/strong> \u5f15\u64ce\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u9009\u62e9\u3002\u4f46\u662f\uff0c\u5982\u679c\u9047\u5230\u9519\u8bef\u201cbadzipfile: file is not a zip file\u201d\uff0c\u5219\u9700\u8981\u4f7f\u7528 <strong>xlrd<\/strong> \u5f15\u64ce\u6765\u8bfb\u53d6\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p>\u4e3a\u4e86\u907f\u514d\u6b64\u95ee\u9898\uff0c\u5728\u4ece\u6587\u4ef6\u5939\u4e2d\u8bfb\u53d6 excel \u6587\u4ef6\u4e4b\u524d\uff0c\u786e\u4fdd\u5173\u95ed\u6240\u6709\u6253\u5f00\u7684 excel \u6587\u4ef6\u5e76\u5220\u9664\u4efb\u4f55\u9690\u85cf\u7684\u4e34\u65f6\u6587\u4ef6 ~$filename.xlsx\u3002\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u53ef\u4ee5\u786e\u4fdd pandas \u548c glob \u7684\u65e0\u9519\u8bef\u5bfc\u5165\u3002<\/p>\n<p><strong>\u66f4\u65b0\u7684\u4ee3\u7801\uff1a<\/strong><\/p>\n<pre># \u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u6307\u5b9a\u5f15\u64ce\ndf = pd.read_excel(f, engine=\"openpyxl\").reindex(columns=customer_id).dropna(how='all', axis=1)\n\n# \u5982\u679c\u51fa\u73b0 BadZipFile \u9519\u8bef\uff0c\u5219\u4f7f\u7528 xlrd\ntry:\n    df = pd.read_excel(f, engine=\"openpyxl\").reindex(columns=customer_id).dropna(how='all', axis=1)\nexcept BadZipFile:\n    df = pd.read_excel(f, engine=\"xlrd\").reindex(columns=customer_id).dropna(how='all', axis=1)<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u4f7f\u7528 Pandas \u548c glob \u5bfc\u5165 Excel \u6587\u4ef6\u65f6\u5982\u4f55\u89e3\u51b3\u201cExcel \u6587\u4ef6\u683c\u5f0f\u65e0\u6cd5\u786e\u5b9a\u201d\u9519\u8bef\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 Pandas \u548c glob&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-205293","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=205293"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/205293\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=205293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=205293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=205293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}