{"id":2228,"date":"2018-07-28T18:23:16","date_gmt":"2018-07-28T23:23:16","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=2228"},"modified":"2018-07-28T18:56:21","modified_gmt":"2018-07-28T23:56:21","slug":"python-open-read-csv-documents","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/07\/28\/python-open-read-csv-documents\/","title":{"rendered":"Python: How to open and read .csv documents"},"content":{"rendered":"<p>Unlike text documents, which Python has a native open() function for:<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"yG8RQd0fCs\"><p><a href=\"http:\/\/bluegalaxy.info\/codewalk\/2018\/07\/28\/python-open-read-text-documents\/\">Python: How to open and read text documents<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" src=\"http:\/\/bluegalaxy.info\/codewalk\/2018\/07\/28\/python-open-read-text-documents\/embed\/#?secret=yG8RQd0fCs\" data-secret=\"yG8RQd0fCs\" width=\"600\" height=\"338\" title=\"&#8220;Python: How to open and read text documents&#8221; &#8212; Chris Nielsen Code Walk\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p>Opening and reading .csv files requires importing the csv module and using a csv.reader(). For example, this code will open and read the first 10 lines in a .csv file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">import csv\r\n\r\ncsv_path = r'C:\\Users\\Chris\\Desktop\\Python Scripts\\Python Class\\Week_2\\code examples\\EEU_071114.csv'\r\n\r\nreader = csv.reader(open(csv_path, 'r'), dialect='excel-tab')\r\n\r\n# Print only the first 10 lines\r\nx = 0\r\nfor line in reader:\r\n    print line\r\n    x += 1\r\n    if x &gt; 10:\r\n        break<\/pre>\n<p>Output:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">['Czech Republic,Czech (CZE),Street Names,\"43,585\"']\r\n[',,Zones,160']\r\n[',,Admins,\"10,349\"']\r\n[',,Signs,\"5,520\"']\r\n[',,POI Names,\"72,297\"']\r\n['Greece,Greek (GRE),Street Names,\"41,138\"']\r\n[',,Zones,\"1,199\"']\r\n[',,Admins,\"3,916\"']\r\n[',,Signs,\"7,009\"']\r\n[',,POI Names,\"60,878\"']\r\n['Hungary,Hungarian (HUN),Street Names,\"33,926\"']<\/pre>\n<p>&nbsp;<\/p>\n<p>Notice this key line:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">reader = csv.reader(open(csv_path, 'r'), dialect='excel-tab')<\/code><\/p>\n<p>The first argument to csv.reader() is <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">open(csv_path, 'r')<\/code> . Just like opening text documents, use open(), with an &#8216;r&#8217; argument for &#8216;read&#8217;. The second argument is <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">dialect='excel-tab'<\/code> .<\/p>\n<p>reader is a csv read object that is iterable, but not accessible by index, so if we want to capture all lines of the csv file in a list, we must first iterate over the reader object. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">import csv\r\n\r\ncsv_path = r'C:\\Users\\Chris\\Desktop\\Python Scripts\\Python Class\\Week_2\\code examples\\EEU_071114.csv'\r\n\r\nreader = csv.reader(open(csv_path, 'r'), dialect='excel-tab')\r\n\r\n# Capture all lines in an array\r\ncsv_doc = []\r\nfor line in reader:\r\n    csv_doc.append(line)\r\n\r\n<\/pre>\n<p>Then if we wanted to print the last line of the csv file we could do so by index:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># Print the last line of the csv document\r\nprint csv_doc[-1]<\/pre>\n<p>which yields:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">[',,POI Names,\"22,450\"']<\/pre>\n<p>Notice that the line is returned as a comma separated string in an array. We can now parse this data by splitting the string by comma. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\"># For the first 5 lines, split each line by comma\r\nx = 0\r\nfor line in reader:\r\n    line = line[0]\r\n    line = line.split(',')\r\n    print line\r\n    x += 1\r\n    if x &gt; 5:\r\n        break<\/pre>\n<p>which yields:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">['Czech Republic', 'Czech (CZE)', 'Street Names', '\"43', '585\"']\r\n['', '', 'Zones', '160']\r\n['', '', 'Admins', '\"10', '349\"']\r\n['', '', 'Signs', '\"5', '520\"']\r\n['', '', 'POI Names', '\"72', '297\"']\r\n['Greece', 'Greek (GRE)', 'Street Names', '\"41', '138\"']<\/pre>\n<p>&nbsp;<br \/>\nFor more information about the csv module, see:<br \/>\n<a href=\"https:\/\/docs.python.org\/2\/library\/csv.html\">https:\/\/docs.python.org\/2\/library\/csv.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Unlike text documents, which Python has a native open() function for: Python: How to open and read text documents Opening and reading .csv files requires importing the csv module and using a csv.reader(). For example, this code will open and read the first 10 lines in a .csv file: import csv csv_path = r&#8217;C:\\Users\\Chris\\Desktop\\Python Scripts\\Python &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/07\/28\/python-open-read-csv-documents\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Python: How to open and read .csv documents<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[8,163,4],"class_list":["post-2228","post","type-post","status-publish","format-standard","hentry","category-python-language","tag-csv","tag-csv-reader","tag-python"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=2228"}],"version-history":[{"count":6,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2228\/revisions"}],"predecessor-version":[{"id":2242,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/2228\/revisions\/2242"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=2228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=2228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=2228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}