[Tut] Convert CSV to Dictionary in Python - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] Convert CSV to Dictionary in Python (/thread-99601.html) |
[Tut] Convert CSV to Dictionary in Python - xSicKxBot - 06-19-2022 Convert CSV to Dictionary in Python <div><div class="kk-star-ratings kksr-valign-top kksr-align-left " data-payload="{"align":"left","id":"425456","slug":"default","valign":"top","reference":"auto","count":"1","readonly":"","score":"5","best":"5","gap":"5","greet":"Rate this post","legend":"5\/5 - (1 vote)","size":"24","width":"142.5","_legend":"{score}\/{best} - ({count} {votes})"}"> <div class="kksr-stars"> <div class="kksr-stars-inactive"> <div class="kksr-star" data-star="1" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="2" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="3" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="4" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="5" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> <div class="kksr-stars-active" style="width: 142.5px;"> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> </div> <div class="kksr-legend"> 5/5 – (1 vote) </div> </div> <div class="wp-block-image"> <figure class="aligncenter size-large"><img loading="lazy" 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="(max-width: 1024px) 100vw, 1024px" /></figure> </div> <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> </p> <p>Let’s have a look at a simple example to demonstrate this solution next!</p> <h2>Basic Solution: CSV to Dict Example</h2> <p>Here’s the content of an example CSV file <code>"my_file.csv"</code> used in our code snippet below:</p> <pre class="wp-block-preformatted"><code>Name,Job,Age,Income Alice,Programmer,23,110000 Bob,Executive,34,90000 Carl,Sales,45,50000</code></pre> <p>If you visualize this CSV in table form, it looks like this:</p> <figure class="wp-block-table is-style-stripes"> <table> <thead> <tr> <th>Name</th> <th>Job</th> <th>Age</th> <th>Income</th> </tr> </thead> <tbody> <tr> <td>Alice</td> <td>Programmer</td> <td>23</td> <td>110000</td> </tr> <tr> <td>Bob</td> <td>Executive</td> <td>34</td> <td>90000</td> </tr> <tr> <td>Carl</td> <td>Sales</td> <td>45</td> <td>50000</td> </tr> </tbody> </table> </figure> <p>Here’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> <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> <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> <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> <pre class="wp-block-preformatted"><code>{'Name': 'Alice', 'Job': 'Programmer', 'Age': '23', 'Income': '110000'} {'Name': 'Bob', 'Job': 'Executive', 'Age': '34', 'Income': '90000'} {'Name': 'Carl', 'Job': 'Sales', 'Age': '45', 'Income': '50000'}</code></pre> </p> <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> <p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1fab2.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note</strong>: A common error is to pass the filename as a string—but this doesn’t work! The <code>csv.DictReader(f)</code> method expects a file object as a required argument.</p> <h2>One-Liner Solution: CSV to Dict</h2> <p>I love Python one-liners. That’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 src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p> <p><strong>So, can we convert a CSV to a list of dictionaries in a single line of Python?</strong></p> <p>Of course, we can!</p> <p>Here’s the one-liner that accomplishes the same as the code discussed before:</p> <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> <p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" 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> <p>The output is the same as before:</p> <pre class="wp-block-preformatted"><code>{'Name': 'Alice', 'Job': 'Programmer', 'Age': '23', 'Income': '110000'} {'Name': 'Bob', 'Job': 'Executive', 'Age': '34', 'Income': '90000'} {'Name': 'Carl', 'Job': 'Sales', 'Age': '45', 'Income': '50000'}</code></pre> <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> <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> <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> <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> <p>If you’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> <h2>Python One-Liners Book: Master the Single Line First!</h2> <p><strong>Python programmers will improve their computer science skills with these useful one-liners.</strong></p> <div class="wp-block-image"> <figure class="aligncenter size-medium is-resized"><a href="https://www.amazon.com/gp/product/B07ZY7XMX8" target="_blank" rel="noopener noreferrer"><img 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="(max-width: 512px) 100vw, 512px" /></a></figure> </div> <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 “one-liners”: <strong><em>concise statements of useful functionality packed into a single line of code. </em></strong>You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.</p> <p>The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms. </p> <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’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> <p>You’ll also learn how to:</p> <ul> <li>Leverage data structures to <strong>solve real-world problems</strong>, like using Boolean indexing to find cities with above-average pollution</li> <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> <li>Calculate basic <strong>statistics </strong>of multidimensional data arrays and the K-Means algorithms for unsupervised learning</li> <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> <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> </ul> <p>By the end of the book, you’ll know how to <strong><em>write Python at its most refined</em></strong>, and create concise, beautiful pieces of “Python art” in merely a single line.</p> <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> </div> https://www.sickgaming.net/blog/2022/06/17/convert-csv-to-dictionary-in-python/ |