{"id":125763,"date":"2022-06-17T09:05:29","date_gmt":"2022-06-17T09:05:29","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=425456"},"modified":"2022-06-17T09:05:29","modified_gmt":"2022-06-17T09:05:29","slug":"convert-csv-to-dictionary-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/06\/17\/convert-csv-to-dictionary-in-python\/","title":{"rendered":"Convert CSV to Dictionary in Python"},"content":{"rendered":"<div class=\"kk-star-ratings kksr-valign-top kksr-align-left \" data-payload=\"{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;425456&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;count&quot;:&quot;1&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/convert_csv_to_dicts-1024x576.jpg\" alt=\"\" class=\"wp-image-425497\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/convert_csv_to_dicts-1024x576.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/convert_csv_to_dicts-300x169.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/convert_csv_to_dicts-768x432.jpg 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/06\/convert_csv_to_dicts.jpg 1280w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<p class=\"has-global-color-8-background-color has-background\">The best way to convert a CSV file to a Python dictionary is to create a CSV file object <code>f<\/code> using <code>open(\"my_file.csv\")<\/code> and pass it in the <code>csv.DictReader(f)<\/code> method. The return value is an iterable of dictionaries, one per row in the CSV file, that maps the column header from the first row to the specific row value.<\/p>\n<\/p>\n<p>Let&#8217;s have a look at a simple example to demonstrate this solution next!<\/p>\n<h2>Basic Solution: CSV to Dict Example<\/h2>\n<p>Here&#8217;s the content of an example CSV file <code>\"my_file.csv\"<\/code> used in our code snippet below:<\/p>\n<pre class=\"wp-block-preformatted\"><code>Name,Job,Age,Income\nAlice,Programmer,23,110000\nBob,Executive,34,90000\nCarl,Sales,45,50000<\/code><\/pre>\n<p>If you visualize this CSV in table form, it looks like this:<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<thead>\n<tr>\n<th>Name<\/th>\n<th>Job<\/th>\n<th>Age<\/th>\n<th>Income<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Alice<\/td>\n<td>Programmer<\/td>\n<td>23<\/td>\n<td>110000<\/td>\n<\/tr>\n<tr>\n<td>Bob<\/td>\n<td>Executive<\/td>\n<td>34<\/td>\n<td>90000<\/td>\n<\/tr>\n<tr>\n<td>Carl<\/td>\n<td>Sales<\/td>\n<td>45<\/td>\n<td>50000<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<p>Here&#8217;s the code to convert that CSV file to multiple dictionaries, one <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-dictionary\/\" data-type=\"post\" data-id=\"5232\" target=\"_blank\">dictionary<\/a> per row by using the <code>csv.DictReader(file)<\/code> function:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"7\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import csv csv_filename = 'my_file.csv' with open(csv_filename) as f: reader = csv.DictReader(f) for row in reader: print(row)<\/pre>\n<p>A <a href=\"https:\/\/blog.finxter.com\/python-dictionary\/\" data-type=\"post\" data-id=\"5232\" target=\"_blank\" rel=\"noreferrer noopener\">dictionary<\/a> is a data structure that maps keys to values.<\/p>\n<p>The output of the previous code snippet shows how the first row of the CSV is used as a header to determine the keys of the dictionary that are mapped to the values defined in the individual rows of the CSV file: <\/p>\n<pre class=\"wp-block-preformatted\"><code>{'Name': 'Alice', 'Job': 'Programmer', 'Age': '23', 'Income': '110000'}\n{'Name': 'Bob', 'Job': 'Executive', 'Age': '34', 'Income': '90000'}\n{'Name': 'Carl', 'Job': 'Sales', 'Age': '45', 'Income': '50000'}<\/code><\/pre>\n<\/p>\n<p>The <code>csv.DictReader(f)<\/code> method takes a file object <code>f<\/code> as an input argument. So, you first need to open the file using the built-in Python <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-open-function\/\" data-type=\"post\" data-id=\"24793\" target=\"_blank\">open()<\/a><\/code> function.<\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1fab2.png\" alt=\"\ud83e\udeb2\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Note<\/strong>: A common error is to pass the filename as a string&#8212;but this doesn&#8217;t work! The <code>csv.DictReader(f)<\/code> method expects a file object as a required argument.<\/p>\n<h2>One-Liner Solution: CSV to Dict<\/h2>\n<p>I love Python one-liners. That&#8217;s why I have written a <a href=\"https:\/\/pythononeliners.com\/\" data-type=\"URL\" data-id=\"https:\/\/pythononeliners.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">book<\/a> on those after all. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f642.png\" alt=\"\ud83d\ude42\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<p><strong>So, can we convert a CSV to a list of dictionaries in a single line of Python?<\/strong><\/p>\n<p>Of course, we can!<\/p>\n<p>Here&#8217;s the one-liner that accomplishes the same as the code discussed before:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import csv; print(*csv.DictReader(open('my_file.csv')), sep='\\n')<\/pre>\n<p class=\"has-base-2-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4a1.png\" alt=\"\ud83d\udca1\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Explanation<\/strong>: We import the <code>csv<\/code> module, use the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-semicolons-how-they-work-and-why-haters-tell-you-to-avoid-them\/\" data-type=\"post\" data-id=\"12600\" target=\"_blank\">semicolon<\/a> <code>;<\/code> to package two statements in one line, <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-unpacking\/\" data-type=\"post\" data-id=\"396420\" target=\"_blank\">unpack<\/a> <code>*<\/code> all rows from the <code>csv.DictReader()<\/code> output in a <code>print<\/code> statement, and use the newline character <code>'\\n'<\/code> as a <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/the-separator-and-end-arguments-of-the-python-print-function\/\" data-type=\"post\" data-id=\"4645\" target=\"_blank\">separator<\/a> between two dictionary rows.<\/p>\n<p>The output is the same as before:<\/p>\n<pre class=\"wp-block-preformatted\"><code>{'Name': 'Alice', 'Job': 'Programmer', 'Age': '23', 'Income': '110000'}\n{'Name': 'Bob', 'Job': 'Executive', 'Age': '34', 'Income': '90000'}\n{'Name': 'Carl', 'Job': 'Sales', 'Age': '45', 'Income': '50000'}<\/code><\/pre>\n<p>If you just want to store the CSV contents in a list of dictionaries rather than <a href=\"https:\/\/blog.finxter.com\/python-print\/\" data-type=\"post\" data-id=\"20731\" target=\"_blank\" rel=\"noreferrer noopener\">printing<\/a> them, you can use the following technique:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import csv; lst=[*csv.DictReader(open('my_file.csv'))]; print(lst)<\/pre>\n<p>The output is a <a href=\"https:\/\/blog.finxter.com\/how-to-create-a-list-of-dictionaries-in-python\/\" data-type=\"post\" data-id=\"10576\" target=\"_blank\" rel=\"noreferrer noopener\">list of dictionaries<\/a>, one per (non-header) row of the original CSV:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">[{'Name': 'Alice', 'Job': 'Programmer', 'Age': '23', 'Income': '110000'}, {'Name': 'Bob', 'Job': 'Executive', 'Age': '34', 'Income': '90000'}, {'Name': 'Carl', 'Job': 'Sales', 'Age': '45', 'Income': '50000'}]<\/pre>\n<p>If you&#8217;re interested in learning <a href=\"https:\/\/blog.finxter.com\/python-one-line-x\/\" data-type=\"post\" data-id=\"10612\" target=\"_blank\" rel=\"noreferrer noopener\">one-liners<\/a> as well, feel free to check out my book:<\/p>\n<h2>Python One-Liners Book: Master the Single Line First!<\/h2>\n<p><strong>Python programmers will improve their computer science skills with these useful one-liners.<\/strong><\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-medium is-resized\"><a href=\"https:\/\/www.amazon.com\/gp\/product\/B07ZY7XMX8\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/3D_cover-1024x944.jpg\" alt=\"Python One-Liners\" class=\"wp-image-10007\" width=\"512\" height=\"472\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/3D_cover-scaled.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/3D_cover-300x277.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/3D_cover-768x708.jpg 768w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/a><\/figure>\n<\/div>\n<p><a href=\"https:\/\/amzn.to\/2WAYeJE\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/amzn.to\/2WAYeJE\"><em>Python One-Liners<\/em> <\/a>will teach you how to read and write &#8220;one-liners&#8221;: <strong><em>concise statements of useful functionality packed into a single line of code. <\/em><\/strong>You&#8217;ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.<\/p>\n<p>The book&#8217;s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms. <\/p>\n<p>Detailed explanations of one-liners introduce <strong><em>key computer science concepts <\/em><\/strong>and<strong><em> boost your coding and analytical skills<\/em><\/strong>. You&#8217;ll learn about advanced Python features such as <em><strong>list comprehension<\/strong><\/em>, <strong><em>slicing<\/em><\/strong>, <strong><em>lambda functions<\/em><\/strong>, <strong><em>regular expressions<\/em><\/strong>, <strong><em>map <\/em><\/strong>and <strong><em>reduce <\/em><\/strong>functions, and <strong><em>slice assignments<\/em><\/strong>. <\/p>\n<p>You&#8217;ll also learn how to:<\/p>\n<ul>\n<li>Leverage data structures to <strong>solve real-world problems<\/strong>, like using Boolean indexing to find cities with above-average pollution<\/li>\n<li>Use <strong>NumPy basics<\/strong> such as <em>array<\/em>, <em>shape<\/em>, <em>axis<\/em>, <em>type<\/em>, <em>broadcasting<\/em>, <em>advanced indexing<\/em>, <em>slicing<\/em>, <em>sorting<\/em>, <em>searching<\/em>, <em>aggregating<\/em>, and <em>statistics<\/em><\/li>\n<li>Calculate basic <strong>statistics <\/strong>of multidimensional data arrays and the K-Means algorithms for unsupervised learning<\/li>\n<li>Create more <strong>advanced regular expressions<\/strong> using <em>grouping <\/em>and <em>named groups<\/em>, <em>negative lookaheads<\/em>, <em>escaped characters<\/em>, <em>whitespaces, character sets<\/em> (and <em>negative characters sets<\/em>), and <em>greedy\/nongreedy operators<\/em><\/li>\n<li>Understand a wide range of <strong>computer science topics<\/strong>, including <em>anagrams<\/em>, <em>palindromes<\/em>, <em>supersets<\/em>, <em>permutations<\/em>, <em>factorials<\/em>, <em>prime numbers<\/em>, <em>Fibonacci <\/em>numbers, <em>obfuscation<\/em>, <em>searching<\/em>, and <em>algorithmic sorting<\/em><\/li>\n<\/ul>\n<p>By the end of the book, you&#8217;ll know how to <strong><em>write Python at its most refined<\/em><\/strong>, and create concise, beautiful pieces of &#8220;Python art&#8221; in merely a single line.<\/p>\n<p><strong><a href=\"https:\/\/amzn.to\/2WAYeJE\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/amzn.to\/2WAYeJE\"><em>Get your Python One-Liners on Amazon!!<\/em><\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) The best way to convert a CSV file to a Python dictionary is to create a CSV file object f using open(&#8220;my_file.csv&#8221;) and pass it in the csv.DictReader(f) method. The return value is an iterable of dictionaries, one per row in the CSV file, that maps the column header from the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-125763","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/125763","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=125763"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/125763\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=125763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=125763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=125763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}