{"id":445360,"date":"2021-04-19T15:37:22","date_gmt":"2021-04-19T07:37:22","guid":{"rendered":"http:\/\/4563.org\/?p=445360"},"modified":"2021-04-19T15:37:22","modified_gmt":"2021-04-19T07:37:22","slug":"%e5%8a%a8%e6%80%81%e5%88%9b%e5%bb%ba%e5%8f%98%e9%87%8f%e5%90%8d%e5%b9%b6%e8%af%bb%e5%8f%96%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=445360","title":{"rendered":"\u52a8\u6001\u521b\u5efa\u53d8\u91cf\u540d\u5e76\u8bfb\u53d6\u6587\u4ef6"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u52a8\u6001\u521b\u5efa\u53d8\u91cf\u540d\u5e76\u8bfb\u53d6\u6587\u4ef6               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : badacook <\/span>  <span><i><\/i> 9<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\">                        \u5927\u5bb6\u597d\uff0c\u8bf7\u6559\u5927\u5bb6\u4e00\u4e2a\u95ee\u9898<br \/>\u573a\u666f\u662f \u60f3\u8bfb\u53d6\u4e00\u4e2a\u76ee\u5f55\u4e0b\u7684 csv \u6587\u4ef6\uff0c\u5c06\u5404\u4e2a\u6587\u4ef6\u8bfb\u53d6\u540e\u8d4b\u503c\u7ed9\u4ee5\u6587\u4ef6\u540d(\u4e0d\u542b\u6269\u5c55\u540d)\u7684\u53d8\u91cf\uff0c\u8d4b\u503c\u8bfb\u53d6\u90a3\u4e00\u884c\u5199\u4e0d\u51fa\u6765\uff0c\u5b9e\u4f8b\u5982\u4e0b<br \/>os.chdir(datapath)<br \/>filelist = os.listdir(datapath)<br \/>for item in filelist:<br \/> if item.endswith(&#8216;.csv&#8217;):<br \/> #\u5148\u5c06 \u4ee5 item \u7684\u6587\u4ef6\u540d\u4e3a\u53d8\u91cf\u540d = pd.read_csv(item)<\/p>\n<p>\u6bd4\u5982\uff1a\u5b58\u5728 a.csv<br \/>a = pd.read_csv(a.csv)      <\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>13<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5644243\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : typetraits <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6587\u4ef6\u540d\u5b58\u8fdb dict\uff0c\u7136\u540e\u8bbf\u95ee dict \u91cc\u8fd9\u4e9b key\uff0c\u8fd9\u79cd\u601d\u8def\u600e\u4e48\u6837                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5644244\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : typetraits <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5c40\u90e8\u53d8\u91cf\u53ef\u4ee5\u901a\u8fc7 locals()\u8bbf\u95ee\uff0c\u5b9e\u9645\u4e5f\u662f\u4e2a dict\uff0c\u6211\u89c9\u5f97\u4f60\u8fd9\u4e2a\u9700\u6c42\u53ef\u4ee5\u76f4\u63a5\u901a\u8fc7\u628a pandas DataFrame \u653e\u8fdb dict \u89e3\u51b3                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5644245\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : minami <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u7528 exec \u51fd\u6570\u6267\u884c\u8d4b\u503c\u8bed\u53e5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5644246\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : aijam <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             globals()[item[:-4]] = pd.read_csv(item)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5644247\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : badacook <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @typetraits \u4e3a\u5565 \u8fd8\u8981\u8f6c\u4e00\u6b65 \uff0c\u80fd\u4e0d\u80fd\u5c31\u5728\u8fd9\u4e2a for \u5185\u90e8 if \u8bed\u53e5\u4e2d\u5b8c\u6210\u554a\uff0c\u8fd8\u8981\u521b\u5efa\u5b57\u5178 \u518d\u5386\u904d\u5b57\u5178\u554a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5644248\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : badacook <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @minami exec \u51fd\u6570\u6267\u884c\u8d4b\u503c\u8bed\u53e5\uff0cexec &#8220;item[:-4]=pd.read_csv(item,encoding=&#8217;gbk&#8217;)&#8221; \uff0c\u5176\u4e2d item \u5982\u4f55\u89e3\u6790\u4e3a\u53d8\u91cf\u554a                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5644249\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : badacook <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @minami <br \/>exstr = &#8220;&#8216;{0}'[:-4] = pd.read_csv({0},encoding=&#8217;gbk&#8217;)&#8221;.format(item)<br \/> exec exstr <br \/>\u6309\u7167 \u4f60\u8bf4\u7684 \u751f\u6210\u6bcf\u4e2a\u6761\u76ee \u7684\u547d\u4ee4\u5b57\u7b26\u4e32\uff0c\u4f7f\u7528 exec \u51fd\u6570\uff0c\u65e0\u6cd5\u6267\u884c \u662f\u5426 exec \u4e0d\u652f\u6301\u53d8\u91cf\u5b57\u7b26\u4e32                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5644250\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : minami <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @badacook #7 exec \u662f\u53ef\u4ee5\u652f\u6301\u7b80\u5355\u7684\u53d8\u91cf\u8d4b\u503c\u7684\uff0c\u6bd4\u5982\u4e0b\u9762\u8fd9\u6837\u3002\u592a\u590d\u6742\u7684\u8bed\u53e5\u5c31\u4e0d\u6e05\u695a\u4e86<br \/>&gt;&gt;&gt; b=1<br \/>&gt;&gt;&gt; exec(&#8216;c=b&#8217;)<br \/>&gt;&gt;&gt; c<br \/>1                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5644251\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : pursuer <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @badacook \u5982\u679c\u662f\u8d4b\u503c\u7ed9\u6a21\u5757\u4f5c\u7528\u57df\u7684\u53d8\u91cf\uff0c\u6309\u7167#4 \u7684\u65b9\u5f0f\u5c31\u53ef\u4ee5\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5644252\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : badacook <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @pursuer \u90a3\u4e2a\u8d4b\u503c \u52a0\u8f7d csv \u5728 for \u7684 if \u5206\u652f\u4e0b\uff0c\u800c\u4e14 \u60f3\u505a\u5230\u53d8\u91cf\u540d\u4e0e\u52a0\u8f7d\u7684 csv \u6587\u4ef6\u540c\u540d\uff0c\u5176\u5b9e\u4f5c\u7528\u57df\u662f\u60f3\u505a\u5230 for \u7684\u5916\u5c42\u4f7f\u7528\uff0c\u53ea\u662f\u60f3\u7740\u770b\u770b \u5728 for \u7684 if \u5206\u652f \u8d4b\u503c\u52a0\u8f7d\u65f6 \u53ef\u4ee5\u52a8\u6001\uff0c\u4e0d\u77e5\u9053\u80fd\u5426\u5b9e\u73b0                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5644253\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : pursuer <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             4#\u63d0\u4f9b\u7684\u90a3\u4e2a globals()[item[:-4]] = pd.read_csv(item)\u5e94\u8be5\u5c31\u53ef\u4ee5\u4e86\u5427\uff0c\u5982\u679c\u4f60\u60f3\u505a\u6210\u4e00\u4e2a\u72ec\u7acb\u7684\u6a21\u5757\uff0c\u7136\u540e\u4fee\u6539\u8c03\u7528\u8005\u7684\u4f5c\u7528\u57df\uff0c\u90a3\u6837\u7684\u8bdd\u9700\u8981\u5c06 global \u4f20\u9012\u8fc7\u53bb\uff0c\u6216\u8005\u8bd5\u7740\u4ece\u8c03\u7528\u6808\u627e\u5230\u8c03\u7528\u8005\u7684\u6a21\u5757\uff0c\u4fee\u6539 sys.modules \u3002\u4f46\u540e\u9762\u8fd9\u4e2a\u6211\u89c9\u5f97\u4e0d\u592a\u597d\uff0c\u53ef\u80fd\u610f\u5916\u7684\u8986\u76d6\u53d8\u91cf                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5644254\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : badacook <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @pursuer \u4f60\u5982\u679c\u6709\u7a7a \u80fd\u4e0d\u80fd\u8bd5\u4e00\u8bd5\uff0c\u5c31\u662f\u4e00\u4e2a\u76ee\u5f55\u4e0b\u6709\u51e0\u4e2a csv \u6587\u4ef6\uff0c\u8fd8\u53ef\u80fd\u6709\u5176\u4ed6\u7c7b\u578b\u6587\u4ef6\uff0c\u8981\u505a\u7684\u5c31\u662f\u4f7f\u7528 pandas \u5e93\uff0c\u5c06\u8fd9\u4e9b csv \u6587\u4ef6 \u4ee5\u6587\u4ef6\u540d\u53d8\u91cf\u7684\u5f62\u5f0f\u8bfb\u8fdb\u53bb\uff0c\u53d8\u91cf\u4f5c\u7528\u57df\u5728\u6574\u4e2a\u811a\u672c\u8303\u56f4\uff0c\u5176\u4e2d globals()\u4e0d\u662f\u8fd4\u56de\u7684\u5168\u5c40\u5b57\u5178\u561b\uff0c\u4f5c\u4e3a python \u65b0\u624b\u7684\u6211 \u5b9e\u5728\u6709\u4e9b\u4e0d\u7406\u89e3\uff0c\u5176\u4e2d exec \u51fd\u6570 \u63d0\u4f9b\u5b57\u7b26\u4e32\u547d\u4ee4\u7684\u6267\u884c\uff0c\u611f\u89c9\u8ddf\u8fd9\u4e2a\u9700\u6c42\u4e5f\u6709\u70b9\u513f\u63a5\u8fd1\uff0c\u80fd\u4e0d\u80fd\u8bd5\u4e00\u8bd5 \u4e5f\u662f\u4e2a\u7b80\u5355\u7684\u5c0f\u5b9e\u9a8c \u6211\u4e5f\u60f3\u901a\u8fc7\u8fd9\u4e2a\u5c0f\u5b9e\u9a8c \u7406\u89e3\u8fd9\u4e2a\u8fc7\u7a0b\uff0c\u8c22\u8c22\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5644255\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : badacook <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @aijam \u975e\u5e38\u611f\u8c22 globals() \u4e0e vars() \u5747\u80fd\u5b9e\u73b0 setting a string as a variable name\uff0c\u6216\u8005\u8bf4 create variable variables in Python                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u52a8\u6001\u521b\u5efa\u53d8\u91cf\u540d\u5e76\u8bfb\u53d6\u6587\u4ef6 \u8cc7\u6df1\u5927\u4f6c&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/445360"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=445360"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/445360\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=445360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=445360"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=445360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}