{"id":623,"date":"2017-03-07T20:10:33","date_gmt":"2017-03-07T11:10:33","guid":{"rendered":"http:\/\/www.python.ambitious-engineer.com\/?p=623"},"modified":"2018-01-15T23:43:09","modified_gmt":"2018-01-15T14:43:09","slug":"csv","status":"publish","type":"post","link":"https:\/\/www.python.ambitious-engineer.com\/archives\/623","title":{"rendered":"csv\u30e2\u30b8\u30e5\u30fc\u30eb CSV\u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u66f8\u304d"},"content":{"rendered":"<p>\u4eca\u56de\u306fCSV\u30d5\u30a1\u30a4\u30eb\u3092\u6271\u3048\u308b\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306ecsv\u30e2\u30b8\u30e5\u30fc\u30eb\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3059\u3002\u96c6\u8a08\u3084\u5206\u6790\u3092\u5834\u5408\u306f\u30b5\u30fc\u30c9\u30d1\u30fc\u30c6\u30a3\u88fd\u306epandas\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3092\u304a\u3059\u3059\u3081\u3057\u307e\u3059\u304c\u3001\u5358\u7d14\u306a\u51e6\u7406\u3067\u3042\u308c\u3070\u3053\u308c\u3082\u5341\u5206\u4f7f\u3048\u307e\u3059\u3002<\/p>\n<h2>csv\u30e2\u30b8\u30e5\u30fc\u30eb<\/h2>\n<p>\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306ecsv\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u4f7f\u7528\u3059\u308b\u3068CSV\u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u66f8\u304d\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h2>CSV\u306e\u8aad\u307f\u8fbc\u307f<\/h2>\n<h3>csv.reader<\/h3>\n<p>csv.reader\u3092\u4f7f\u7528\u3057\u3066CSV\u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u8fbc\u307f\u3092\u884c\u3044\u307e\u3059\u3002\u4ee5\u4e0b\u306e\u30b5\u30f3\u30d7\u30eb\u306f\u3001\u30ab\u30ec\u30f3\u30c8\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u3042\u308bdata.csv\u3068\u3044\u3046CSV\u30d5\u30a1\u30a4\u30eb\u30921\u884c\u305a\u3064\u30ea\u30b9\u30c8\u3067\u51fa\u529b\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport csv\r\n\r\n# CSV\u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u8fbc\u307f\r\nwith open('data.csv', 'r') as f:\r\n    reader = csv.reader(f) # reader\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u751f\u6210\r\n    print(type(reader)) # reader\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u306flist\u578b\r\n    for row in reader:\r\n        # 1\u884c\u305a\u3064\u30ea\u30b9\u30c8\u3067\u51fa\u529b\u3055\u308c\u308b\r\n        print(row)\r\n<\/pre>\n<h3>\u30d8\u30c3\u30c0\u30fc\u3092\u8aad\u307f\u98db\u3070\u3059<\/h3>\n<p>\u307e\u305f\u3001\u30d8\u30c3\u30c0\u30fc\u4ed8\u304d\u306eCSV\u30d5\u30a1\u30a4\u30eb\u3067\u30d8\u30c3\u30c0\u30fc\u3092\u8aad\u307f\u98db\u3070\u3057\u305f\u308a\u30eb\u30fc\u30d7\u524d\u306b\u53d6\u5f97\u3057\u305f\u3044\u5834\u5408\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306bnext\u95a2\u6570\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">    reader = csv.reader(f)\r\n    header = next(reader) # list\u306b\u683c\u7d0d\u3055\u308c\u308b\u305f\u3081\u3001\u8aad\u307f\u98db\u3070\u3057\u305f\u3044\u5834\u5408\u306fnext\u3092\u4f7f\u7528\u3059\u308b\r\n    for row in reader:\r\n        print(row)\r\n\r\n<\/pre>\n<h3>TSV\u30d5\u30a1\u30a4\u30eb \u533a\u5207\u308a\u6587\u5b57\u3092\u5909\u3048\u308b<\/h3>\n<p>TSV\u30d5\u30a1\u30a4\u30eb\u7b49\u3067\u30ab\u30f3\u30de\u4ee5\u5916\u306e\u533a\u5207\u308a\u6587\u5b57\u3092\u4f7f\u7528\u3057\u305f\u3044\u5834\u5408\u306f\u5f15\u6570\u3067delimiter\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport csv\r\n \r\n# TSV\u30d5\u30a1\u30a4\u30eb\u306e\u8aad\u307f\u8fbc\u307f\r\nwith open('data.tsv', 'r') as f:\r\n    reader = csv.reader(f, delimiter='\\t') # delimiter\u3092\u6307\u5b9a\r\n    for row in reader:\r\n        print(row)\r\n<\/pre>\n<h2>CSV\u30d5\u30a1\u30a4\u30eb\u306e\u66f8\u304d\u8fbc\u307f<\/h2>\n<p>\u30ea\u30b9\u30c8\u30c7\u30fc\u30bf\u3092CSV\u30d5\u30a1\u30a4\u30eb\u3068\u3057\u3066\u51fa\u529b\u3059\u308b\u3053\u3068\u3082\u53ef\u80fd\u3067\u3059\u3002[[1\u884c\u76ee][2\u884c\u76ee][3\u884c\u76ee][4\u884c\u76ee]...[\u6700\u7d42\u884c\u76ee]]\u3068\u3044\u3063\u305f\u5f62\u5f0f\u3067\u3001\u5165\u308c\u5b50\u306e\u30ea\u30b9\u30c8\u3067\u30c7\u30fc\u30bf\u3092\u6307\u5b9a\u3057\u3001csv.writer\u3067\u51fa\u529b\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport csv \r\n \r\n# CSV\u30d5\u30a1\u30a4\u30eb\u306e\u66f8\u304d\u8fbc\u307f\r\nlist_data = [['a', 'b', 'c'], [1, 2, 3], [4, 5, 6]]\r\nwith open('output.csv', 'w', newline='') as f:\r\n    writer = csv.writer(f)\r\n    writer.writerows(list_data)\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u56de\u306fCSV\u30d5\u30a1\u30a4\u30eb\u3092\u6271\u3048\u308b\u6a19\u6e96\u30e9\u30a4\u30d6\u30e9\u30ea\u306ecsv\u30e2\u30b8\u30e5\u30fc\u30eb\u306b\u3064\u3044\u3066\u89e3\u8aac\u3057\u307e\u3059\u3002\u96c6\u8a08\u3084\u5206\u6790\u3092\u5834\u5408\u306f\u30b5\u30fc\u30c9\u30d1\u30fc\u30c6\u30a3\u88fd\u306epandas\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3092\u304a\u3059\u3059\u3081\u3057\u307e\u3059\u304c\u3001\u5358\u7d14\u306a\u51e6\u7406\u3067\u3042\u308c\u3070\u3053\u308c\u3082\u5341\u5206\u4f7f\u3048\u307e\u3059\u3002 csv\u30e2\u30b8\u30e5\u30fc\u30eb ...<\/p>\n","protected":false},"author":1,"featured_media":3574,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[211],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/623"}],"collection":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/comments?post=623"}],"version-history":[{"count":5,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/623\/revisions"}],"predecessor-version":[{"id":3578,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/posts\/623\/revisions\/3578"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media\/3574"}],"wp:attachment":[{"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/media?parent=623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/categories?post=623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.python.ambitious-engineer.com\/wp-json\/wp\/v2\/tags?post=623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}