{"id":9554,"date":"2015-12-29T16:11:18","date_gmt":"2015-12-29T14:11:18","guid":{"rendered":"http:\/\/www.webcodegeeks.com\/?p=9554"},"modified":"2018-01-09T09:52:04","modified_gmt":"2018-01-09T07:52:04","slug":"python-csv-reader-writer-example","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/","title":{"rendered":"Python CSV Reader \/ Writer Example"},"content":{"rendered":"<p>CSV (comma-separated values) is a standard for exporting and importing data across multiple formats, such as MySQL and excel.<\/p>\n<p>It stores numbers and text in plain text. Each row of the file is a data record and every record consists of one or more fields which values are separated by commas. The use of the comma to separate every record&#8217;s fields is the source of the name given to this standard.<\/p>\n<p>Even with this\u00a0<strong>very\u00a0<\/strong>explicit name, there is no official standard CSVs, and it may denote some very similar\u00a0<em>delimiter-separated values,<\/em> which use a variety of field delimiters (such as spaces and tabs, which are both very popular), and are given the <em>.csv\u00a0<\/em>extension anyway. Such lack of strict terminology makes data exchange very difficult some times.<\/p>\n<p><a href=\"https:\/\/www.ietf.org\/rfc\/rfc4180.txt\" target=\"_blank\">RFC 4180<\/a>\u00a0provides some rules to this format:<\/p>\n<ul>\n<li>It&#8217;s plain text<\/li>\n<li>Consists of records<\/li>\n<li>Every record consists of fields separated by a single character delimiter<\/li>\n<li>Every record has the same sequence of fields<\/li>\n<\/ul>\n<p>But unless there is additional information about the provided file (such as if the rules provided by RFC were followed), data exchange through this format can be pretty annoying.<br \/>\n&nbsp;<br \/>\n[ulp id=&#8217;R7QVpFMZmjosABLC&#8217;]<\/p>\n<h2>1. The Basics<\/h2>\n<p>Python has native support for CSV readers, and it&#8217;s configurable (which, as we&#8217;ve seen, is necessary). There is a\u00a0module\u00a0<em>csv\u00a0<\/em>which holds everything you need to make a CSV reader\/writer, and it follows RFC standards (unless your configuration overrides them), so by default it should read and write valid CSV files.<br \/>\nSo, let&#8217;s see how it works:<\/p>\n<p><span style=\"text-decoration: underline\"><em>csv-reader.py<\/em><\/span><\/p>\n<pre class=\"brush:python; wrap-lines:false\">\r\nimport csv\r\nwith open('my.csv', 'r+', newline='') as csv_file:\r\n    reader = csv.reader(csv_file)\r\n    for row in reader:\r\n        print(str(row))\r\n<\/pre>\n<p>Here, we are importing\u00a0<em>csv\u00a0<\/em>and opening a file called\u00a0<em>&#8216;my.csv&#8217;<\/em>, then we call\u00a0<em>csv.reader<\/em> passing our file as a parameter and then we print each row in our reader.<\/p>\n<p>If\u00a0<em>&#8216;my.csv&#8217;<\/em> looks like this:<\/p>\n<p><span style=\"text-decoration: underline\"><em>my.csv<\/em><\/span><\/p>\n<pre class=\"brush:bash; wrap-lines:false\">\r\nmy first column,my second column,my third column\r\nmy first column 2,my second column 2,my third column 2\r\n<\/pre>\n<p>Then, when you run this script, you will see the following output:<\/p>\n<pre class=\"brush:bash; wrap-lines:false\">\r\n['my first column', 'my second column', 'my third column']\r\n['my first column 2', 'my second column 2', 'my third column 2']\r\n<\/pre>\n<p>And writing is just as simple as reading:<\/p>\n<p><span style=\"text-decoration: underline\"><em>csv-reader.py<\/em><\/span><\/p>\n<pre class=\"brush:python; wrap-lines:false\">\r\nimport csv\r\nrows = [['1', '2', '3'], ['4', '5', '6']]\r\nwith open('my.csv', 'w+', newline='') as csv_file:\r\n    writer = csv.writer(csv_file)\r\n    for row in rows:\r\n        writer.writerow(row)\r\n\r\nwith open('my.csv', 'r+', newline='') as csv_file:\r\n    reader = csv.reader(csv_file)\r\n    for row in reader:\r\n        print(str(row))\r\n<\/pre>\n<p>Then, in your csv file you&#8217;ll see:<\/p>\n<p><span style=\"text-decoration: underline\"><em>my.csv<\/em><\/span><\/p>\n<pre class=\"brush:bash; wrap-lines:false\">\r\n1,2,3\r\n4,5,6\r\n<\/pre>\n<p>And in your output:<\/p>\n<pre class=\"brush:bash; wrap-lines:false\">\r\n['1', '2', '3']\r\n['4', '5', '6']\r\n<\/pre>\n<p>It&#8217;s pretty easy to see what is going on in here. We are opening a file in write mode, getting our writer from <em>csv<\/em> giving our file to it, and writing each row with it. Making it a little smarter:<\/p>\n<p><span style=\"text-decoration: underline\"><em>csv-reader.py<\/em><\/span><\/p>\n<pre class=\"brush:python; wrap-lines:false\">\r\nimport csv\r\n\r\n\r\ndef read(file_location):\r\n    with open(file_location, 'r+', newline='') as csv_file:\r\n        reader = csv.reader(csv_file)\r\n        return [row for row in reader]\r\n\r\n\r\ndef write(file_location, rows):\r\n    with open(file_location, 'w+', newline='') as csv_file:\r\n        writer = csv.writer(csv_file)\r\n        for row in rows:\r\n            writer.writerow(row)\r\n\r\n\r\ndef raw_test():\r\n    columns = int(input(\"How many columns do you want to write? \"))\r\n    input_rows = []\r\n    keep_going = True\r\n    while keep_going:\r\n        input_rows.append([input(\"column {}: \".format(i + 1)) for i in range(0, columns)])\r\n        ui_keep_going = input(\"continue? (y\/N): \")\r\n        if ui_keep_going != \"y\":\r\n            keep_going = False\r\n\r\n    print(str(input_rows))\r\n\r\n    write('raw.csv', input_rows)\r\n    written_value = read('raw.csv')\r\n    print(str(written_value))\r\n\r\nraw_test()\r\n<\/pre>\n<p>We ask the user how many columns does he want to write for each row and then ask him for a row as long as he wants to continue, then we print our raw input and write it to a file called <em>raw.csv<\/em>, then we read it again and print the data. When we run our script, the output will look like this:<\/p>\n<pre class=\"brush:bash; wrap-lines:false\">\r\nHow many columns do you want to write? 3\r\ncolumn 1: row 1, column 1\r\ncolumn 2: row 1, column 2\r\ncolumn 3: row 1, column 3\r\ncontinue? (y\/N): y\r\ncolumn 1: row 2, column 1\r\ncolumn 2: row 2, column 2\r\ncolumn 3: row 3, column 3\r\ncontinue? (y\/N): \r\n[['row 1, column 1', 'row 1, column 2', 'row 1, column 3'], ['row 2, column 1', 'row 2, column 2', 'row 3, column 3']]\r\n[['row 1, column 1', 'row 1, column 2', 'row 1, column 3'], ['row 2, column 1', 'row 2, column 2', 'row 3, column 3']]\r\n\r\nProcess finished with exit code 0\r\n<\/pre>\n<p>And, of course, our <em>raw.csv<\/em> looks like this:<\/p>\n<p><span style=\"text-decoration: underline\"><em>raw.csv<\/em><\/span><\/p>\n<pre class=\"brush:bash; wrap-lines:false\">\r\n\"row 1, column 1\",\"row 1, column 2\",\"row 1, column 3\"\r\n\"row 2, column 1\",\"row 2, column 2\",\"row 3, column 3\"\r\n<\/pre>\n<p>Another rule the CSV format has, is the quote character. As you see, every input has a comma, which is our separator character, so the writer puts them between quoting marks (the default of the standard) to know that commas between them are not separators, but part of the column instead.<\/p>\n<p>Now, although I would recommend leaving the configuration with its defaults, there are some cases where you need to change them, as you don&#8217;t always have control over the csv&#8217;s your data providers give you. So, I have to teach you how to do it (beware, great powers come with great responsibilities).<\/p>\n<p>You can configure the delimiter and the quote character through <code>delimiter<\/code> and <code>quotechar<\/code> parameters, like this:<\/p>\n<p><span style=\"text-decoration: underline\"><em>csv-reader.py<\/em><\/span><\/p>\n<pre class=\"brush:python; wrap-lines:false\">\r\nimport csv\r\n\r\n\r\ndef read(file_location):\r\n    with open(file_location, 'r+', newline='') as csv_file:\r\n        reader = csv.reader(csv_file, delimiter=' ', quotechar='|')\r\n        return [row for row in reader]\r\n\r\n\r\ndef write(file_location, rows):\r\n    with open(file_location, 'w+', newline='') as csv_file:\r\n        writer = csv.writer(csv_file, delimiter=' ', quotechar='|')\r\n        for row in rows:\r\n            writer.writerow(row)\r\n\r\n\r\ndef raw_test():\r\n    columns = int(input(\"How many columns do you want to write? \"))\r\n    input_rows = []\r\n    keep_going = True\r\n    while keep_going:\r\n        input_rows.append([input(\"column {}: \".format(i + 1)) for i in range(0, columns)])\r\n        ui_keep_going = input(\"continue? (y\/N): \")\r\n        if ui_keep_going != \"y\":\r\n            keep_going = False\r\n\r\n    print(str(input_rows))\r\n\r\n    write('raw.csv', input_rows)\r\n    written_value = read('raw.csv')\r\n    print(str(written_value))\r\n\r\nraw_test()\r\n<\/pre>\n<p>So, now, having this console output:<\/p>\n<pre class=\"brush:bash; wrap-lines:false\">\r\nHow many columns do you want to write? 3\r\ncolumn 1: row 1 column 1\r\ncolumn 2: row 1 column 2\r\ncolumn 3: row 1 column 3\r\ncontinue? (y\/N): y\r\ncolumn 1: row 2 column 1\r\ncolumn 2: row 2 column 2\r\ncolumn 3: row 2 column 3\r\ncontinue? (y\/N): \r\n[['row 1 column 1', 'row 1 column 2', 'row 1 column 3'], ['row 2 column 1', 'row 2 column 2', 'row 2 column 3']]\r\n[['row 1 column 1', 'row 1 column 2', 'row 1 column 3'], ['row 2 column 1', 'row 2 column 2', 'row 2 column 3']]\r\n<\/pre>\n<p>Our <em>raw.csv<\/em> will like like this:<\/p>\n<p><span style=\"text-decoration: underline\"><em>raw.csv<\/em><\/span><\/p>\n<pre class=\"brush:bash; wrap-lines:false\">\r\n|row 1 column 1| |row 1 column 2| |row 1 column 3|\r\n|row 2 column 1| |row 2 column 2| |row 2 column 3|\r\n<\/pre>\n<p>As you see, our new separator is the <em>space<\/em> character, and our quote character is <em>pipe<\/em>, which our writer is forced to use always as the <em>space<\/em> character is pretty common in almost every text data.<\/p>\n<p>The writer&#8217;s quoting strategy is also configurable, the values available are:<\/p>\n<ul>\n<li><code>csv.QUOTE_ALL<\/code>: quotes every column, it doesn&#8217;t matter if they contain a delimiter character or not.<\/li>\n<li><code>csv.QUOTE_MINIMAL<\/code>: quotes only the columns which contains a delimiter character.<\/li>\n<li><code>csv.QUOTE_NONNUMERIC<\/code>: quotes all non numeric columns.<\/li>\n<li><code>csv.QUOTE_NONE<\/code>: quotes nothing. It forces you to check whether or not the user inputs a delimiter character in a column, if you don&#8217;t, you will read an unexpected number of columns.<\/li>\n<\/ul>\n<h2>2. Reading and writing dictionaries<\/h2>\n<p>We&#8217;ve seen a very basic example of how to read and write data from a CSV file, but in real life, we don&#8217;t want our CSV&#8217;s to be so chaotic, we need them to give us information about what meaning has each of the columns. <\/p>\n<p>Also, en real life we don&#8217;t usually have our data in arrays, we have business models and we need them to be very descriptive. We usually use dictionaries for this purpose, and python gives us the tools to write and read dictionaries from CSV files.<\/p>\n<p>It looks like this:<\/p>\n<pre class=\"brush:python; wrap-lines:false\">\r\nimport csv\r\n\r\ndictionaries = [{'age': '30', 'name': 'John', 'last_name': 'Doe'}, {'age': '30', 'name': 'Jane', 'last_name': 'Doe'}]\r\nwith open('my.csv', 'w+') as csv_file:\r\n    headers = [k for k in dictionaries[0]]\r\n    writer = csv.DictWriter(csv_file, fieldnames=headers)\r\n    writer.writeheader()\r\n    for dictionary in dictionaries:\r\n        writer.writerow(dictionary)\r\n\r\nwith open('my.csv', 'r+') as csv_file:\r\n    reader = csv.DictReader(csv_file)\r\n    print(str([row for row in reader]))\r\n<\/pre>\n<p>We are initializing a variable called <code>dictionaries<\/code> with an array of test data, then we open a file in write mode, we collect the keys of our dictionary and get a writer of our file with the headers. The first thing we do is write our headers, and then write a row for every dictionary in our array.<\/p>\n<p>Then we open the same file in read mode, get a reader of that file and print the array of data. You will see an output like:<\/p>\n<pre class=\"brush:bash; wrap-lines:false\">\r\n[{'name': 'John', 'age': '30', 'last_name': 'Doe'}, {'name': 'Jane', 'age': '30', 'last_name': 'Doe'}]\r\n<\/pre>\n<p>And our csv file will look like:<\/p>\n<p><span style=\"text-decoration: underline\"><em>my.csv<\/em><\/span><\/p>\n<pre class=\"brush:bash; wrap-lines:false\">\r\nname,last_name,age\r\nJohn,Doe,30\r\nJane,Doe,30\r\n<\/pre>\n<p>Now it looks better. The CSV file has our headers information, and each row has the ordered sequence of our data. Notice that we give the file names to our writer, as dictionaries in python are not ordered so the writer needs that information to write each row with the same order.<\/p>\n<p>The same parameters apply for the delimiter and the quote character as the default reader and writer.<\/p>\n<p>So, then again, we make it a little smarter:<\/p>\n<p><span style=\"text-decoration: underline\"><em>csv-reader.py<\/em><\/span><\/p>\n<pre class=\"brush:python; wrap-lines:false\">\r\nimport csv\r\n\r\ndef read_dict(file_location):\r\n    with open(file_location, 'r+') as csv_file:\r\n        reader = csv.DictReader(csv_file)\r\n        print(str([row for row in reader]))\r\n        return [row for row in reader]\r\n\r\n\r\ndef write_dict(file_location, dictionaries):\r\n    with open(file_location, 'w+') as csv_file:\r\n        headers = [k for k in dictionaries[0]]\r\n        writer = csv.DictWriter(csv_file, fieldnames=headers)\r\n        writer.writeheader()\r\n        for dictionary in dictionaries:\r\n            writer.writerow(dictionary)\r\n\r\n\r\ndef dict_test():\r\n    input_rows = []\r\n    keep_going = True\r\n    while keep_going:\r\n        name = input(\"Name: \")\r\n        last_name = input(\"Last Name: \")\r\n        age = input(\"Age: \")\r\n        input_rows.append({\"name\": name, \"last_name\": last_name, \"age\": age})\r\n        ui_keep_going = input(\"continue? (y\/N): \")\r\n        if ui_keep_going != \"y\":\r\n            keep_going = False\r\n\r\n    print(str(input_rows))\r\n\r\n    write_dict('dict.csv', input_rows)\r\n    written_value = read_dict('dict.csv')\r\n    print(str(written_value))\r\n\r\ndict_test()\r\n<\/pre>\n<p>And now when we run this script, we&#8217;ll see the output:<\/p>\n<pre class=\"brush:bash; wrap-lines:false\">\r\nName: John\r\nLast Name: Doe\r\nAge: 30\r\ncontinue? (y\/N): y\r\nName: Jane\r\nLast Name: Doe\r\nAge: 40\r\ncontinue? (y\/N): \r\n[{'age': '30', 'last_name': 'Doe', 'name': 'John'}, {'age': '40', 'last_name': 'Doe', 'name': 'Jane'}]\r\n[{'age': '30', 'last_name': 'Doe', 'name': 'John'}, {'age': '40', 'last_name': 'Doe', 'name': 'Jane'}]\r\n<\/pre>\n<p>And our <em>dict.csv<\/em> will look like:<\/p>\n<p><span style=\"text-decoration: underline\"><em>csv-reader.py<\/em><\/span><\/p>\n<pre class=\"brush:bash; wrap-lines:false\">\r\nage,last_name,name\r\n30,Doe,John\r\n40,Doe,Jane\r\n<\/pre>\n<p>A little side note: As I said before, dictionaries in python are not ordered, so when you extract the keys from one to write its data to a CSV file you should order them to have your columns ordered always the same way, as you do not know which technology will your client use to read them, and, of course, in real life CSV files are incremental, so you are always adding lines to them, not overriding them. Avoid any trouble making sure your CSV will always look the same.<\/p>\n<h2>3. Download the Code Project<\/h2>\n<p>This was an example on how to read and write data from\/to a CSV file.<\/p>\n<div class=\"download\">\n    <strong>Download<\/strong><br \/> You can download the full source code of this example here: <a href=\"http:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2015\/12\/csv.zip\"><strong>python-csv-reader<\/strong><\/a>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>CSV (comma-separated values) is a standard for exporting and importing data across multiple formats, such as MySQL and excel. It stores numbers and text in plain text. Each row of the file is a data record and every record consists of one or more fields which values are separated by commas. The use of the &hellip;<\/p>\n","protected":false},"author":109,"featured_media":1651,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[],"class_list":["post-9554","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python CSV Reader \/ Writer Example - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"CSV (comma-separated values) is a standard for exporting and importing data across multiple formats, such as MySQL and excel. It stores numbers and text\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python CSV Reader \/ Writer Example - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"CSV (comma-separated values) is a standard for exporting and importing data across multiple formats, such as MySQL and excel. It stores numbers and text\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/sgvinci\" \/>\n<meta property=\"article:published_time\" content=\"2015-12-29T14:11:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-01-09T07:52:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Sebastian Vinci\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@sebastianvinci_\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sebastian Vinci\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/\"},\"author\":{\"name\":\"Sebastian Vinci\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/06a43c63e373dff2e159bbc029b405aa\"},\"headline\":\"Python CSV Reader \/ Writer Example\",\"datePublished\":\"2015-12-29T14:11:18+00:00\",\"dateModified\":\"2018-01-09T07:52:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/\"},\"wordCount\":1124,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/\",\"name\":\"Python CSV Reader \/ Writer Example - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"datePublished\":\"2015-12-29T14:11:18+00:00\",\"dateModified\":\"2018-01-09T07:52:04+00:00\",\"description\":\"CSV (comma-separated values) is a standard for exporting and importing data across multiple formats, such as MySQL and excel. It stores numbers and text\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/python\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Python CSV Reader \/ Writer Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/06a43c63e373dff2e159bbc029b405aa\",\"name\":\"Sebastian Vinci\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/12d0233b49dd2a282330a987b16e81c3fbd4a8a8f5d5338348a6edd47cfff99e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/12d0233b49dd2a282330a987b16e81c3fbd4a8a8f5d5338348a6edd47cfff99e?s=96&d=mm&r=g\",\"caption\":\"Sebastian Vinci\"},\"description\":\"Sebastian is a full stack programmer, who has strong experience in Java and Scala enterprise web applications. He is currently studying Computers Science in UBA (University of Buenos Aires) and working a full time job at a .com company as a Semi-Senior developer, involving architectural design, implementation and monitoring. He also worked in automating processes (such as data base backups, building, deploying and monitoring applications).\",\"sameAs\":[\"http:\/\/www.webcodegeeks.com\/\",\"https:\/\/www.facebook.com\/sgvinci\",\"https:\/\/x.com\/sebastianvinci_\"],\"url\":\"https:\/\/www.webcodegeeks.com\/author\/sebastian-vinci\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python CSV Reader \/ Writer Example - Web Code Geeks - 2026","description":"CSV (comma-separated values) is a standard for exporting and importing data across multiple formats, such as MySQL and excel. It stores numbers and text","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:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/","og_locale":"en_US","og_type":"article","og_title":"Python CSV Reader \/ Writer Example - Web Code Geeks - 2026","og_description":"CSV (comma-separated values) is a standard for exporting and importing data across multiple formats, such as MySQL and excel. It stores numbers and text","og_url":"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_author":"https:\/\/www.facebook.com\/sgvinci","article_published_time":"2015-12-29T14:11:18+00:00","article_modified_time":"2018-01-09T07:52:04+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","type":"image\/jpeg"}],"author":"Sebastian Vinci","twitter_card":"summary_large_image","twitter_creator":"@sebastianvinci_","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Sebastian Vinci","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/"},"author":{"name":"Sebastian Vinci","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/06a43c63e373dff2e159bbc029b405aa"},"headline":"Python CSV Reader \/ Writer Example","datePublished":"2015-12-29T14:11:18+00:00","dateModified":"2018-01-09T07:52:04+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/"},"wordCount":1124,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/","url":"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/","name":"Python CSV Reader \/ Writer Example - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","datePublished":"2015-12-29T14:11:18+00:00","dateModified":"2018-01-09T07:52:04+00:00","description":"CSV (comma-separated values) is a standard for exporting and importing data across multiple formats, such as MySQL and excel. It stores numbers and text","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/11\/python-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/python\/python-csv-reader-writer-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Python","item":"https:\/\/www.webcodegeeks.com\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python CSV Reader \/ Writer Example"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/06a43c63e373dff2e159bbc029b405aa","name":"Sebastian Vinci","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/12d0233b49dd2a282330a987b16e81c3fbd4a8a8f5d5338348a6edd47cfff99e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/12d0233b49dd2a282330a987b16e81c3fbd4a8a8f5d5338348a6edd47cfff99e?s=96&d=mm&r=g","caption":"Sebastian Vinci"},"description":"Sebastian is a full stack programmer, who has strong experience in Java and Scala enterprise web applications. He is currently studying Computers Science in UBA (University of Buenos Aires) and working a full time job at a .com company as a Semi-Senior developer, involving architectural design, implementation and monitoring. He also worked in automating processes (such as data base backups, building, deploying and monitoring applications).","sameAs":["http:\/\/www.webcodegeeks.com\/","https:\/\/www.facebook.com\/sgvinci","https:\/\/x.com\/sebastianvinci_"],"url":"https:\/\/www.webcodegeeks.com\/author\/sebastian-vinci\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/9554","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/109"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=9554"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/9554\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/1651"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=9554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=9554"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=9554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}