{"id":839,"date":"2024-09-03T21:35:55","date_gmt":"2024-09-03T16:35:55","guid":{"rendered":"https:\/\/augurytech.co.uk\/courses\/?p=839"},"modified":"2024-10-05T09:04:21","modified_gmt":"2024-10-05T04:04:21","slug":"python-csv-files","status":"publish","type":"post","link":"https:\/\/augurytech.co.uk\/courses\/python\/python-csv-files\/","title":{"rendered":"Python: CSV Files"},"content":{"rendered":"\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<p><strong>CSV files are text-only files that are simplified versions of a spreadsheet or database.<\/strong><\/p>\n\n\n\n<p><strong>&#8220;CSV&#8221; stands for Comma-Separated Values.<\/strong><\/p>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading has--font-size\">export the Excel file as a CSV file.<\/h3>\n\n\n\n<p><strong>The CSV file looks like this:<\/strong><br>Year, Event, Winner<br>1995, Best-Kept Lawn, None<br>1999, Gobstones, Welch National<br>2006, World Cup, Burkina Faso<\/p>\n\n\n\n<p><strong>Each row of the spreadsheet is a separate line in the CSV file.<\/strong><\/p>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading has--font-size\">Reading in a CSV file<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import csv\nwith open(\"competitions.csv\") as f:\n   contents_of_file = csv.reader(f)<\/code><\/pre>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<p class=\"has--font-size\">The contents of the CSV file returned by the <strong>csv.reader<\/strong> function aren&#8217;t useable yet. You must loop through the data stored in <strong>contents_of_f<\/strong> line by line, adding each line to a list.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"competitions.csv\") as f:\n   contents_of_f= csv.reader(f)\n   potter_competitions = &#91;]\n\n   for each line in contents_of_f:\n       potter_competitions += each_line<\/code><\/pre>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading has--font-size\">Writing to a CSV file<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"whatever.csv\", \"w\", newline=\"\") as f:\n   data_handler = csv.writer(f, delimiter=\",\")\n   data_handler.writerow(&#91;\"Year\", \"Event\", \"Winner\"])\n   data_handler.writerow(&#91;\"1995\", \"Best-Kept Lawn\",\"None\"])\n   data_handler.writerow(&#91;\"1999\", \"Gobstones\",\"W Nation!\"])<\/code><\/pre>\n<\/div>\n\n\n\n<div style=\"height:50px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<div class=\"wp-block-group is-vertical is-content-justification-stretch is-layout-flex wp-container-core-group-is-layout-353c4f5a wp-block-group-is-layout-flex\">\n<h3 class=\"wp-block-heading has--font-size\">Appending to a CSV file<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>with open(\"whatever.csv\", \"a\", newline=\"\") as f:\n   data_handler = csv.writer(f, delimiter=\",\")\n   data_handler.writerow(&#91;\"Year\", \"Event\", \"Winner\"])\n   data_handler.writerow(&#91;\"1995\", \"Best-Kept Lawn\",\"None\"])\n   data_handler.writerow(&#91;\"1999\", \"Gobstones\",\"W Nationl\"])<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>CSV files are text-only files that are simplified versions of a spreadsheet or database. &#8220;CSV&#8221; stands for Comma-Separated Values. export the Excel file as a CSV file. The CSV file looks like this:Year, Event, Winner1995, Best-Kept Lawn, None1999, Gobstones, Welch National2006, World Cup, Burkina Faso Each row of the spreadsheet is a separate line in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"wp-custom-template-python","format":"standard","meta":{"_seopress_robots_primary_cat":"none","_seopress_titles_title":"","_seopress_titles_desc":"","_seopress_robots_index":"","footnotes":""},"categories":[5],"tags":[],"class_list":{"0":"post-839","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-python"},"_links":{"self":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/839","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/comments?post=839"}],"version-history":[{"count":15,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/839\/revisions"}],"predecessor-version":[{"id":872,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/posts\/839\/revisions\/872"}],"wp:attachment":[{"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/media?parent=839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/categories?post=839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/augurytech.co.uk\/courses\/wp-json\/wp\/v2\/tags?post=839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}