{"id":169216,"date":"2022-04-09T13:43:35","date_gmt":"2022-04-09T05:43:35","guid":{"rendered":"http:\/\/www.idc.net\/help\/169216\/"},"modified":"2022-04-09T13:43:35","modified_gmt":"2022-04-09T05:43:35","slug":"%e5%90%8e%e6%b5%aa%e4%ba%91python%e6%95%99%e7%a8%8b%ef%bc%9apython%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5csv%e6%96%87%e4%bb%b6%e6%a0%bc%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/169216\/","title":{"rendered":"\u540e\u6d6a\u4e91Python\u6559\u7a0b\uff1apython\u5982\u4f55\u5bfc\u5165csv\u6587\u4ef6\u683c\u5f0f"},"content":{"rendered":"<p><strong><\/strong><\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/oss.py.cn\/pycn\/upload\/article\/000\/000\/003\/5d8588882c9cb110.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>1\u3001\u901a\u8fc7\u6807\u51c6\u7684Python\u5e93\u5bfc\u5165CSV\u6587\u4ef6<\/strong><\/p>\n<p>Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u6807\u51c6\u7684\u7c7b\u5e93CSV\u6587\u4ef6\u3002\u8fd9\u4e2a\u7c7b\u5e93\u4e2d\u7684reader()\u51fd\u6570\u7528\u6765\u5bfc\u5165CSV\u6587\u4ef6\u3002\u5f53CSV\u6587\u4ef6\u88ab\u8bfb\u5165\u540e\uff0c\u53ef\u4ee5\u5229\u7528\u8fd9\u4e9b\u6570\u636e\u751f\u6210\u4e00\u4e2aNumPy\u6570\u7ec4\uff0c\u7528\u6765\u8bad\u7ec3\u7b97\u6cd5\u6a21\u578b\u3002<\/p>\n<pre>from&nbsp;csv&nbsp;importreader\nimport&nbsp;numpy&nbsp;as&nbsp;np\nfilename=input(\"\u8bf7\u8f93\u5165\u6587\u4ef6\u540d\uff1a&nbsp;\")\nwithopen(filename,'rt',encoding='UTF-8')as&nbsp;raw_data:\n&nbsp;&nbsp;&nbsp;&nbsp;readers=reader(raw_data,delimiter=',')\n&nbsp;&nbsp;&nbsp;&nbsp;x=list(readers)\n&nbsp;&nbsp;&nbsp;&nbsp;data=np.array(x)\n&nbsp;&nbsp;&nbsp;&nbsp;print(data)\n&nbsp;&nbsp;&nbsp;&nbsp;print(data.shape)<\/pre>\n<p>\u76f8\u5173\u63a8\u8350\uff1a\u300aPython\u57fa\u7840\u6559\u7a0b\u300b<\/p>\n<p><strong>2\u3001\u901a\u8fc7NumPy\u5bfc\u5165CSV\u6587\u4ef6<\/strong><\/p>\n<p>\u4e5f\u53ef\u4ee5\u4f7f\u7528NumPy\u7684loadtxt()\u51fd\u6570\u5bfc\u5165\u6570\u636e\u3002\u4f7f\u7528\u8fd9\u4e2a\u51fd\u6570\u5904\u7406\u7684\u6570\u636e\u6ca1\u6709\u6587\u4ef6\u5934\uff0c\u5e76\u4e14\u6240\u6709\u7684\u6570\u636e\u7ed3\u6784\u90fd\u662f\u4e00\u6837\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u6570\u636e\u7c7b\u578b\u662f\u4e00\u6837\u7684\u3002<\/p>\n<pre>from&nbsp;numpy&nbsp;importloadtxt\nfilename=input(\"\u6587\u4ef6\u540d\uff1a\")\nwithopen(filename,'rt',encoding='UTF-8')as&nbsp;raw_data:\n&nbsp;&nbsp;&nbsp;&nbsp;data=loadtxt(raw_data,delimiter=',')\n&nbsp;&nbsp;&nbsp;&nbsp;print(data)<\/pre>\n<p><strong>3\u3001\u901a\u8fc7Pandas\u5bfc\u5165CSV\u6587\u4ef6<\/strong><\/p>\n<p>\u901a\u8fc7Pandas\u6765\u5bfc\u5165CSV\u6587\u4ef6\u8981\u4f7f\u7528pandas.read_csv()\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662fDataFrame\uff0c\u53ef\u4ee5\u5f88\u65b9\u4fbf\u7684\u8fdb\u884c\u4e0b\u4e00\u6b65\u7684\u5904\u7406\uff0c\u5b9e\u9645\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\u63a8\u8350\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<p>\u5728\u673a\u5668\u5b66\u4e60\u7684\u9879\u76ee\u4e2d\uff0c\u7ecf\u5e38\u5229\u7528Pandas\u6765\u505a\u6570\u636e\u6e05\u6d17\u4e0e\u6570\u636e\u51c6\u5907\u5de5\u4f5c\u3002<\/p>\n<pre>from&nbsp;pandas&nbsp;importread_csv\nfilename=input(\"\u6587\u4ef6\u540d\uff1a\")\nf=open(filename,encoding='UTF-8')\nnames=['\u4f5c\u4e1a\u65e5\u671f','\u03b7CO','\u03b7H2','TF(\u2103)','TC(\u2103)','mass','\u9001\u98ce\u6d41\u91cf']\ndata=read_csv(f,names=names)\nprint(data)<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u901a\u8fc7\u6807\u51c6\u7684Python\u5e93\u5bfc\u5165CSV\u6587\u4ef6 Python\u63d0\u4f9b\u4e86\u4e00\u4e2a\u6807\u51c6\u7684\u7c7b\u5e93CSV\u6587\u4ef6\u3002\u8fd9\u4e2a\u7c7b\u5e93\u4e2d\u7684reade [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":169217,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[182397],"tags":[],"class_list":["post-169216","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/169216","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=169216"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/169216\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/169217"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=169216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=169216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=169216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}