{"id":502,"date":"2023-02-23T15:55:00","date_gmt":"2023-02-23T15:55:00","guid":{"rendered":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/?page_id=502"},"modified":"2023-02-28T08:59:18","modified_gmt":"2023-02-28T08:59:18","slug":"comma-separated-values-csv","status":"publish","type":"page","link":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-4\/comma-separated-values-csv\/","title":{"rendered":"Comma separated values (CSV)"},"content":{"rendered":"\n<p>A common task in programming is working with CSV data. Writing is easy, just add a comma between the data on a row, but how do you read in a CSV file?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method 1<\/h2>\n\n\n\n<p>Use the split function to break the string when it finds a comma.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>data = &#091;]\nwith open(\"data.csv\") as data_file:\n    file_lines = data_file.read().splitlines()\n    for line in file_lines:\n        data.append(line.split(','))<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Method 2<\/h2>\n\n\n\n<p>Import the csv module and use the reader method to go through each line.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import csv\ndata = &#091;]\nwith open(\"data.csv\") as data_file:\n    file_lines = csv.reader(data_file)\n    for line in file_lines:\n        data.append(line)<\/code><\/pre>\n\n\n\n<p>In the above examples you would end up with an array where each element contains an array representing a row of data from the file. So print(data[0][1]) would print the second column in the first row of the csv file.<\/p>\n\n\n\n<p class=\"nextlink\"><a href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-4\/challenge-9\/\" data-type=\"page\" data-id=\"509\">Next: Challenge 9<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A common task in programming is working with CSV data. Writing is easy, just add a comma between the data on a row, but how do you read in a CSV file? Method 1 Use the split function to break the string when it finds a comma. Method 2 Import the csv module and use&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.glowscotland.org.uk\/es\/software\/software-design-and-development\/week-4\/comma-separated-values-csv\/\">Continue reading <span class=\"screen-reader-text\">Comma separated values (CSV)<\/span><\/a><\/p>\n","protected":false},"author":5710,"featured_media":0,"parent":20,"menu_order":5,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-502","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/502","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/users\/5710"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/comments?post=502"}],"version-history":[{"count":4,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/502\/revisions"}],"predecessor-version":[{"id":512,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/502\/revisions\/512"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/pages\/20"}],"wp:attachment":[{"href":"https:\/\/blogs.glowscotland.org.uk\/es\/software\/wp-json\/wp\/v2\/media?parent=502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}