{"id":998,"date":"2018-05-22T09:28:58","date_gmt":"2018-05-22T09:28:58","guid":{"rendered":"http:\/\/learnlearn.uk\/python\/?page_id=998"},"modified":"2023-11-29T13:36:43","modified_gmt":"2023-11-29T13:36:43","slug":"python-csv-files-tutorial","status":"publish","type":"page","link":"https:\/\/learnlearn.uk\/python\/python-csv-files-tutorial\/","title":{"rendered":"Python CSV Files Tutorial"},"content":{"rendered":"<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">CSV \u2192 2D List<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Reading a CSV File into a 2 Dimensional Python List<\/h3>\n<p>Sometimes you will need to read data stored in <span style=\"color: #0000ff;\">comma separated value<\/span> (CSV) files. You can use the csv module in order to achieve this.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000; font-weight: bold;\">import<\/span> <span style=\"color: #0000ff; font-weight: bold;\">csv<\/span>\r\n\r\nf <span style=\"color: #666666;\">=<\/span> <span style=\"color: #008000;\">open<\/span>(<span style=\"color: #ba2121;\">'mycsvfile.csv'<\/span>,<span style=\"color: #ba2121;\">'r'<\/span>)\r\nreader <span style=\"color: #666666;\">=<\/span> csv<span style=\"color: #666666;\">.<\/span>reader(f, delimiter <span style=\"color: #666666;\">=<\/span> <span style=\"color: #ba2121;\">','<\/span>)\r\n\r\nitems <span style=\"color: #666666;\">=<\/span> []\r\n<span style=\"color: #008000; font-weight: bold;\">for<\/span> row <span style=\"color: #aa22ff; font-weight: bold;\">in<\/span> reader:\r\n     items<span style=\"color: #666666;\">.<\/span>append(row)\r\n<\/pre>\n<\/div>\n<p>CSV files usually use a comma as the<strong> delimiter<\/strong> between each data item, but sometimes might use a tab or other character instead.<\/p>\n<h3>Tutorial Video<\/h3>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/bJDZ1wNKGEw\" width=\"860\" height=\"600\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p><a href=\"https:\/\/drive.google.com\/file\/d\/1-3aBKPaxiZzuQ8PCyuIvNFvs-amQvRKE\/view?usp=sharing\">YouTube Blocked? Try the Google Drive Version<\/a><\/p>\n\n<\/div><h2 class=\"tabtitle\">2D List \u2192 CSV<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Writing a 2 Dimensional List to a CSV File<br \/>\n<!-- HTML generated using hilite.me --><\/h3>\n<p>Often you will want to be able to save 2 Dimensional List data to a CSV file so that it isn&#8217;t deleted when your program exits. you can use csv writer to achieve this.<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #ffffff; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #0000aa;\">import<\/span> <span style=\"color: #00aaaa; text-decoration: underline;\">csv<\/span>\r\nf = <span style=\"color: #00aaaa;\">open<\/span>(<span style=\"color: #aa5500;\">'animals.csv'<\/span>,<span style=\"color: #aa5500;\">'w'<\/span>, newline = <span style=\"color: #aa5500;\">''<\/span>)\r\n<span style=\"color: #aaaaaa; font-style: italic;\">#use 'a' instead of 'w' if you want to add to the csv, not write a new one.<\/span>\r\nwriter = csv.writer(f)\r\n<span style=\"color: #aaaaaa; font-style: italic;\"># example 2D List<\/span>\r\nl = [[<span style=\"color: #aa5500;\">\"dog\"<\/span>,<span style=\"color: #009999;\">2<\/span>],[<span style=\"color: #aa5500;\">\"cat\"<\/span>,<span style=\"color: #009999;\">15<\/span>],[<span style=\"color: #aa5500;\">\"mouse\"<\/span>,<span style=\"color: #009999;\">34<\/span>]]\r\n\r\n<span style=\"color: #0000aa;\">for<\/span> item <span style=\"color: #0000aa;\">in<\/span> l:\r\n    \r\n    writer.writerow(item)\r\n\r\nf.close() <span style=\"color: #aaaaaa; font-style: italic;\">#really important!!!<\/span>\r\n<\/pre>\n<\/div>\n<h3>Tutorial Video<\/h3>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/DXzEijPCRc8\" width=\"860\" height=\"600\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p><a href=\"https:\/\/drive.google.com\/file\/d\/1hqp3KGwyMVsasWBi0-OOevdWf3BVGfA5\/view?usp=sharing\">YouTube Blocked? Use the Google Drive version instead<\/a><\/p>\n<pre><\/pre>\n\n<\/div><h2 class=\"tabtitle\">CSV \u2192 Dictionary<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Reading a CSV File into a Python Dictionary<\/h3>\n<p>Sometimes you want to read the data into a dictionary instead of a 2 Dimensional list. CSV reader can do this too.<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000; font-weight: bold;\">import<\/span> <span style=\"color: #0000ff; font-weight: bold;\">csv<\/span>\r\n\r\nf <span style=\"color: #666666;\">=<\/span> <span style=\"color: #008000;\">open<\/span>(<span style=\"color: #ba2121;\">'people.csv'<\/span>,<span style=\"color: #ba2121;\">'r'<\/span>)\r\nreader <span style=\"color: #666666;\">=<\/span> csv<span style=\"color: #666666;\">.<\/span>reader(f, delimiter <span style=\"color: #666666;\">=<\/span> <span style=\"color: #ba2121;\">','<\/span>)\r\n\r\npeople <span style=\"color: #666666;\">=<\/span> {}\r\n<span style=\"color: #008000; font-weight: bold;\">for<\/span> row <span style=\"color: #aa22ff; font-weight: bold;\">in<\/span> reader:\r\n     people[row[<span style=\"color: #666666;\">0<\/span>]] <span style=\"color: #666666;\">=<\/span> {<span style=\"color: #ba2121;\">'age'<\/span>:row[<span style=\"color: #666666;\">1<\/span>],<span style=\"color: #ba2121;\">'color'<\/span>:row[<span style=\"color: #666666;\">2<\/span>]}\r\n<\/pre>\n<\/div>\n<h3>Tutorial Video<\/h3>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/zvrRI0r6f2Q\" width=\"860\" height=\"600\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\" data-mce-fragment=\"1\"><\/iframe><\/p>\n<p><a href=\"https:\/\/drive.google.com\/file\/d\/1W0ALqBJoD-tQF3Sdkhymy_ofjJrPtwDf\/view?usp=sharing\">Can&#8217;t access YouTube? Try the Google Drive Version.<\/a><\/p>\n\n<\/div><h2 class=\"tabtitle\">Dictionary \u2192  CSV<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Writing a Python Dictionary to a CSV File<\/h3>\n<p><!-- HTML generated using hilite.me --><\/p>\n<div style=\"background: #f8f8f8; overflow: auto; width: auto; border: solid gray; border-width: .1em .1em .1em .8em; padding: .2em .6em;\">\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008000; font-weight: bold;\">import<\/span> <span style=\"color: #0000ff; font-weight: bold;\">csv<\/span>\r\n\r\ncols <span style=\"color: #666666;\">=<\/span> [<span style=\"color: #ba2121;\">\"name\"<\/span>,<span style=\"color: #ba2121;\">\"strength\"<\/span>,<span style=\"color: #ba2121;\">\"speed\"<\/span>]\r\n\r\nplayers <span style=\"color: #666666;\">=<\/span> [\r\n    {<span style=\"color: #ba2121;\">\"name\"<\/span>:<span style=\"color: #ba2121;\">\"bob\"<\/span>,<span style=\"color: #ba2121;\">\"strength\"<\/span>:<span style=\"color: #666666;\">45<\/span>,<span style=\"color: #ba2121;\">\"speed\"<\/span>:<span style=\"color: #666666;\">25.5<\/span>},\r\n    {<span style=\"color: #ba2121;\">\"name\"<\/span>:<span style=\"color: #ba2121;\">\"sally\"<\/span>,<span style=\"color: #ba2121;\">\"strength\"<\/span>:<span style=\"color: #666666;\">22<\/span>,<span style=\"color: #ba2121;\">\"speed\"<\/span>:<span style=\"color: #666666;\">50<\/span>}\r\n]\r\n\r\nf <span style=\"color: #666666;\">=<\/span> <span style=\"color: #008000;\">open<\/span>(<span style=\"color: #ba2121;\">\"players.csv\"<\/span>, <span style=\"color: #ba2121;\">\"w\"<\/span>, newline<span style=\"color: #666666;\">=<\/span><span style=\"color: #ba2121;\">''<\/span>)\r\nwriter <span style=\"color: #666666;\">=<\/span> csv<span style=\"color: #666666;\">.<\/span>DictWriter(f, fieldnames <span style=\"color: #666666;\">=<\/span> cols)\r\nwriter<span style=\"color: #666666;\">.<\/span>writeheader() <span style=\"color: #408080; font-style: italic;\">#If you want a header row in your csv<\/span>\r\nwriter<span style=\"color: #666666;\">.<\/span>writerows(players)\r\nf<span style=\"color: #666666;\">.<\/span>close()\r\n<\/pre>\n<\/div>\n\n<\/div><h2 class=\"tabtitle\">Challenge 37<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Challenge 37 &#8211; Animal Stats Printer<\/h3>\n<p><strong><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-886\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/bronze-star25.png\" alt=\"\" width=\"25\" height=\"25\" \/>\u00a0Bro<\/strong><strong>nze<\/strong><\/p>\n<ul>\n<li>Read the CSV file <a href=\"https:\/\/learnlearn.uk\/wp-content\/uploads\/2022\/12\/animal_data.csv\">animal_data.csv <\/a>into a 2 dimensional list and print out the items.<\/li>\n<\/ul>\n<p><strong><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-887\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/silver-star25.png\" alt=\"\" width=\"25\" height=\"25\" \/>\u00a0Silver<\/strong><\/p>\n<ul>\n<li>Print out the list in order of population<\/li>\n<\/ul>\n<p><strong><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-888\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/gold-star-25.png\" alt=\"\" width=\"25\" height=\"25\" \/>\u00a0 Gold<\/strong><\/p>\n<ul>\n<li>Ask the user to type in a column selection(e.g. &#8220;average age&#8221;) and have the program sort the items in that order.<\/li>\n<\/ul>\n\n<\/div><h2 class=\"tabtitle\">38<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Challenge 38 &#8211; Animal Stats Adder<\/h3>\n<p><strong><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-886\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/bronze-star25.png\" alt=\"\" width=\"25\" height=\"25\" \/>\u00a0Bro<\/strong><strong>nze<\/strong><\/p>\n<ul>\n<li>Write a program that allows you to add an extra animal to the animal_facts.csv file. (you might want to use append &#8216;a&#8217; mode instead of\u00a0 write &#8216;w&#8217; mode.)<\/li>\n<\/ul>\n<p><strong><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-887\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/silver-star25.png\" alt=\"\" width=\"25\" height=\"25\" \/>\u00a0Silver<\/strong><\/p>\n<ul>\n<li>Allow the user to keep adding more animals until the type an empty string.<\/li>\n<\/ul>\n<p><strong><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-888\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/gold-star-25.png\" alt=\"\" width=\"25\" height=\"25\" \/>\u00a0 Gold<\/strong><\/p>\n<ul>\n<li>Once the user has finished adding, print out the animals in alphabetical order.<\/li>\n<\/ul>\n\n<\/div><h2 class=\"tabtitle\">39<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Challenge 39 \u2013 IT Support Log<\/h3>\n<p><strong>Bronze<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-874\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/bronze-star-150x150.png\" alt=\"\" width=\"29\" height=\"29\" srcset=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/bronze-star-150x150.png 150w, https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/bronze-star.png 274w\" sizes=\"(max-width: 29px) 100vw, 29px\" \/>\u00a0 Ask the user to input their name and a description of their IT issue. Save the issue to the file \u2018ict_issue_log.txt\u2019<\/p>\n<p><strong>Silver<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-875\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/silver-star-150x150.png\" alt=\"\" width=\"25\" height=\"25\" srcset=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/silver-star-150x150.png 150w, https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/silver-star.png 274w\" sizes=\"(max-width: 25px) 100vw, 25px\" \/>\u00a0Adapt your program so that it automatically adds the<a href=\"https:\/\/learnlearn.uk\/python\/using-date-time-python\/\">\u00a0current date and time to the log.<\/a><\/p>\n<p><strong>Gold<\/strong><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-876\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/gold-star-150x150.png\" alt=\"\" width=\"25\" height=\"25\" srcset=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/gold-star-150x150.png 150w, https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/gold-star.png 274w\" sizes=\"(max-width: 25px) 100vw, 25px\" \/>\u00a0Adapt your program so that each ticket is given a unique job number that increments automatically.<\/p>\n\n<\/div><h2 class=\"tabtitle\">40<\/h2>\n<div class=\"tabcontent\">\n\n<h3>Challenge 40 &#8211; Country Top Trumps<\/h3>\n<p><strong><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-886\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/bronze-star25.png\" alt=\"\" width=\"25\" height=\"25\" \/>\u00a0Bro<\/strong><strong>nze<\/strong><\/p>\n<p>Create program that reads the <a href=\"https:\/\/learnlearn.uk\/wp-content\/uploads\/2022\/12\/country_csv.csv\">country_csv<\/a> file into a list of dictionaries.<\/p>\n<p><strong><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-887\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/silver-star25.png\" alt=\"\" width=\"25\" height=\"25\" \/>\u00a0Silver<\/strong><\/p>\n<p>Create a one player top trumps game where the player picks a single card and the computer picks a card and then they see who wins.<\/p>\n<p><strong><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-888\" src=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/gold-star-25.png\" alt=\"\" width=\"25\" height=\"25\" \/>\u00a0 Gold<\/strong><\/p>\n<p>Extend the game so that you play a full game of top trumps with a deck of card and two players<\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Reading a CSV File into a 2 Dimensional Python List Sometimes you will need to read data stored in comma separated value (CSV) files. You can use the csv module in order to achieve this. import csv f = open(&#8216;mycsvfile.csv&#8217;,&#8217;r&#8217;) reader = csv.reader(f, delimiter = &#8216;,&#8217;) items = [] for row in reader: items.append(row) CSV&hellip;&nbsp;<a href=\"https:\/\/learnlearn.uk\/python\/python-csv-files-tutorial\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Python CSV Files Tutorial<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"on","neve_meta_content_width":87,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python CSV Files Tutorial - Python<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/learnlearn.uk\/python\/python-csv-files-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python CSV Files Tutorial - Python\" \/>\n<meta property=\"og:description\" content=\"Reading a CSV File into a 2 Dimensional Python List Sometimes you will need to read data stored in comma separated value (CSV) files. You can use the csv module in order to achieve this. import csv f = open(&#039;mycsvfile.csv&#039;,&#039;r&#039;) reader = csv.reader(f, delimiter = &#039;,&#039;) items = [] for row in reader: items.append(row) CSV&hellip;&nbsp;Read More &raquo;Python CSV Files Tutorial\" \/>\n<meta property=\"og:url\" content=\"https:\/\/learnlearn.uk\/python\/python-csv-files-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"Python\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-29T13:36:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/bronze-star25.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/learnlearn.uk\/python\/python-csv-files-tutorial\/\",\"url\":\"https:\/\/learnlearn.uk\/python\/python-csv-files-tutorial\/\",\"name\":\"Python CSV Files Tutorial - Python\",\"isPartOf\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/#website\"},\"datePublished\":\"2018-05-22T09:28:58+00:00\",\"dateModified\":\"2023-11-29T13:36:43+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/python-csv-files-tutorial\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/learnlearn.uk\/python\/python-csv-files-tutorial\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/learnlearn.uk\/python\/python-csv-files-tutorial\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Python Unit Home\",\"item\":\"https:\/\/learnlearn.uk\/python\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python CSV Files Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/learnlearn.uk\/python\/#website\",\"url\":\"https:\/\/learnlearn.uk\/python\/\",\"name\":\"Python\",\"description\":\"Programming\",\"publisher\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/learnlearn.uk\/python\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/learnlearn.uk\/python\/#organization\",\"name\":\"Python\",\"url\":\"https:\/\/learnlearn.uk\/python\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png\",\"contentUrl\":\"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png\",\"width\":710,\"height\":98,\"caption\":\"Python\"},\"image\":{\"@id\":\"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python CSV Files Tutorial - Python","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/learnlearn.uk\/python\/python-csv-files-tutorial\/","og_locale":"en_GB","og_type":"article","og_title":"Python CSV Files Tutorial - Python","og_description":"Reading a CSV File into a 2 Dimensional Python List Sometimes you will need to read data stored in comma separated value (CSV) files. You can use the csv module in order to achieve this. import csv f = open('mycsvfile.csv','r') reader = csv.reader(f, delimiter = ',') items = [] for row in reader: items.append(row) CSV&hellip;&nbsp;Read More &raquo;Python CSV Files Tutorial","og_url":"https:\/\/learnlearn.uk\/python\/python-csv-files-tutorial\/","og_site_name":"Python","article_modified_time":"2023-11-29T13:36:43+00:00","og_image":[{"url":"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2017\/09\/bronze-star25.png"}],"twitter_card":"summary_large_image","twitter_misc":{"Estimated reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/learnlearn.uk\/python\/python-csv-files-tutorial\/","url":"https:\/\/learnlearn.uk\/python\/python-csv-files-tutorial\/","name":"Python CSV Files Tutorial - Python","isPartOf":{"@id":"https:\/\/learnlearn.uk\/python\/#website"},"datePublished":"2018-05-22T09:28:58+00:00","dateModified":"2023-11-29T13:36:43+00:00","breadcrumb":{"@id":"https:\/\/learnlearn.uk\/python\/python-csv-files-tutorial\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/learnlearn.uk\/python\/python-csv-files-tutorial\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/learnlearn.uk\/python\/python-csv-files-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Python Unit Home","item":"https:\/\/learnlearn.uk\/python\/"},{"@type":"ListItem","position":2,"name":"Python CSV Files Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/learnlearn.uk\/python\/#website","url":"https:\/\/learnlearn.uk\/python\/","name":"Python","description":"Programming","publisher":{"@id":"https:\/\/learnlearn.uk\/python\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/learnlearn.uk\/python\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-GB"},{"@type":"Organization","@id":"https:\/\/learnlearn.uk\/python\/#organization","name":"Python","url":"https:\/\/learnlearn.uk\/python\/","logo":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/","url":"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png","contentUrl":"https:\/\/learnlearn.uk\/python\/wp-content\/uploads\/sites\/4\/2019\/03\/LearnLearnLogowhite.png","width":710,"height":98,"caption":"Python"},"image":{"@id":"https:\/\/learnlearn.uk\/python\/#\/schema\/logo\/image\/"}}]}},"rttpg_featured_image_url":null,"rttpg_author":{"display_name":"learnlearnadmin","author_link":"https:\/\/learnlearn.uk\/python\/author\/learnlearnadmin\/"},"rttpg_comment":0,"rttpg_category":null,"rttpg_excerpt":"Reading a CSV File into a 2 Dimensional Python List Sometimes you will need to read data stored in comma separated value (CSV) files. You can use the csv module in order to achieve this. import csv f = open('mycsvfile.csv','r') reader = csv.reader(f, delimiter = ',') items = [] for row in reader: items.append(row) CSV&hellip;&nbsp;Read&hellip;","_links":{"self":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages\/998"}],"collection":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/comments?post=998"}],"version-history":[{"count":33,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages\/998\/revisions"}],"predecessor-version":[{"id":1478,"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/pages\/998\/revisions\/1478"}],"wp:attachment":[{"href":"https:\/\/learnlearn.uk\/python\/wp-json\/wp\/v2\/media?parent=998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}