{"id":1100,"date":"2020-07-19T00:31:03","date_gmt":"2020-07-18T16:31:03","guid":{"rendered":"http:\/\/cnliutz.uicp.io\/?p=1100"},"modified":"2020-07-21T18:56:13","modified_gmt":"2020-07-21T10:56:13","slug":"python%e8%af%bb%e5%86%99-csv%e6%a0%bc%e5%bc%8f%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"http:\/\/qbtobjow.ipyingshe.net:5347\/?p=1100","title":{"rendered":"python\u8bfb\u5199.csv\u683c\u5f0f\u6587\u4ef6"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#win10 vscode \u5982\u679cprint\u8f93\u51fa\u4e2d\u6587\u4e71\u7801\uff0c\u8bf7\u8bbe\u7f6e\u73af\u5883\u53d8\u91cfPYTHONIOENCODING=UTF8\n#read csvfile\n#import xlrd\n#import xlwt\nimport csv\nimport numpy as np\nfilename='timu0.csv'\nwith open(filename) as f:\n    r1 = csv.reader(f)\n    #print(list(r1))\n    for row in r1:\n        print(r1.line_num,row,end=\"\\n\")\nprint(\"I love \u6211\u7231\u4f60\uff01\")\n#\u5199\u5165csv\u6570\u636ewrite csv data to a file.\ndatas=&#91;&#91;'\u5546\u54c1\u7f16\u53f7', '\u5546\u54c1\u540d\u79f0', '\u5355\u4ef7', '\u5e93\u5b58', '\u9500\u91cf'],\n&#91;'1', '\u732b\u96f6\u98df', '12', '3133', '5164'],\n&#91;'2', '\u666e\u901a\u732b\u7cae', '33', '5055', '2231'],\n&#91;'3', '\u732b\u7cae\u56db\u5408\u4e00', '187', '212', '334']]\nwith open('d:\/python\/file.csv',\"w\",newline='') as w:\n    w1=csv.writer(w)\n    for row in datas:\n        w1.writerow(row)    #\u6bcf\u6b21\u5199\u5165\u4e00\u884c\n    w1.writerows(datas)     #\u4e00\u6b21\u6027\u628adatas\u5199\u5165\u6587\u4ef6\n#numpy savetxt\uff08\uff09\u5199\u5165\uff0cloadtxt\uff08\uff09\u8bfb\u53d6csv\u6587\u4ef6\na=np.arange(20).reshape(5,4)\nb=np.savetxt('txt_a.csv',a,fmt=\"%d\",delimiter=\",\")\nprint(a)\na_array=np.loadtxt(\"txt_a.csv\",dtype=str,delimiter=None)\nprint(a_array)\n<\/code><\/pre>\n\n\n\n<!--nextpage-->\n\n\n\n<h2 class=\"wp-block-heading\">python openpyxl\u64cd\u4f5cexcel\u8868<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>#openpyxl using method\nfrom openpyxl import Workbook\nfrom openpyxl.worksheet.table import Table, TableStyleInfo\n\nwb = Workbook()\nws = wb.active\n\ndata = &#91;\n    &#91;'Apples', 10000, 5000, 8000, 6000],\n    &#91;'Pears',   2000, 3000, 4000, 5000],\n    &#91;'Bananas', 6000, 6000, 6500, 6000],\n    &#91;'Oranges',  500,  300,  200,  700],\n]\n\n# add column headings. NB. these must be strings\nws.append(&#91;\"Fruit\", \"2011\", \"2012\", \"2013\", \"2014\"])\nfor row in data:\n    ws.append(row)\n\ntab = Table(displayName=\"Table1\", ref=\"A1:E5\")\n\n# Add a default style with striped rows and banded columns\nstyle = TableStyleInfo(name=\"TableStyleMedium9\", showFirstColumn=False,\n                       showLastColumn=False, showRowStripes=True, showColumnStripes=True)\ntab.tableStyleInfo = style\n\n'''\nTable must be added using ws.add_table() method to avoid duplicate names.\nUsing this method ensures table name is unque through out defined names and all other table name. \n'''\nws.add_table(tab)\nwb.save(\"table.xlsx\")<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1100","post","type-post","status-publish","format-standard","hentry","category-2"],"_links":{"self":[{"href":"http:\/\/qbtobjow.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/1100","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/qbtobjow.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/qbtobjow.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/qbtobjow.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/qbtobjow.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1100"}],"version-history":[{"count":5,"href":"http:\/\/qbtobjow.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/1100\/revisions"}],"predecessor-version":[{"id":1109,"href":"http:\/\/qbtobjow.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/1100\/revisions\/1109"}],"wp:attachment":[{"href":"http:\/\/qbtobjow.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1100"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/qbtobjow.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1100"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/qbtobjow.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1100"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}