Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] 7 Best Ways to Convert Dict to CSV in Python

#1
7 Best Ways to Convert Dict to CSV in Python

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload="{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;570412&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&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;,&quot;font_factor&quot;:&quot;1.25&quot;}">
<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" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</div>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: How to convert a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" data-type="URL" data-id="https://blog.finxter.com/python-dictionary/" target="_blank">dictionary</a> to a CSV in Python? </p>
<p>In Python, convert a dictionary to a CSV file <strong>using the <code>DictWriter()</code> method</strong> from the <code>csv</code> module. The <code>csv.DictWriter()</code> method allows you to insert a dictionary-formatted row (keys=column names; values=row elements) into the CSV file using its <code>DictWriter.writerow()</code> method. </p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Learn More</strong>: If you want to learn about converting <em>a list</em> of dictionaries to a CSV, <a href="https://blog.finxter.com/how-to-convert-a-list-of-dicts-to-a-csv-file-in-python/" data-type="post" data-id="557892">check out this Finxter tutorial</a>.</p>
<h2>Method 1: Python Dict to CSV in Pandas</h2>
<p>First, <strong>convert a list of dictionaries</strong> to a<a rel="noreferrer noopener" href="https://pandas.pydata.org/" target="_blank"> Pandas</a> DataFrame that provides you with powerful capabilities such as the </p>
<p>Second, convert the <strong>Pandas DataFrame to a CSV file</strong> using the DataFrame’s <a rel="noreferrer noopener" href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html" target="_blank"><code>to_csv()</code> method</a> with the filename as argument.</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="">salary = [{'Name':'Alice', 'Job':'Data Scientist', 'Salary':122000}, {'Name':'Bob', 'Job':'Engineer', 'Salary':77000}, {'Name':'Carl', 'Job':'Manager', 'Salary':119000}] # Method 1
import pandas as pd
df = pd.DataFrame(salary)
df.to_csv('my_file.csv', index=False, header=True)
</pre>
<p>Output:</p>
<pre class="wp-block-preformatted"><code>Name,Job,Salary
Alice,Data Scientist,122000
Bob,Engineer,77000
Carl,Manager,119000</code></pre>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="572" height="428" src="https://blog.finxter.com/wp-content/uploads/2022/08/image-9.png" alt="" class="wp-image-559621" srcset="https://blog.finxter.com/wp-content/uploads/2022/08/image-9.png 572w, https://blog.finxter.com/wp-content/uplo...00x224.png 300w" sizes="(max-width: 572px) 100vw, 572px" /></figure>
</div>
<p>You create a <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-create-a-dataframe-in-pandas/" data-type="post" data-id="16764" target="_blank">Pandas DataFrame</a>—which is Python’s default representation of tabular data. Think of it as an Excel spreadsheet within your code (with rows and columns). </p>
<p>The DataFrame is a very powerful data structure that allows you to perform various methods. One of those is the <code><a href="https://blog.finxter.com/pandas-to_csv/" data-type="post" data-id="8027" target="_blank" rel="noreferrer noopener">to_csv()</a></code> method that allows you to write its contents into a CSV file.</p>
<ul>
<li>You set the <code>index</code> argument of the <code>to_csv()</code> method to <code>False</code> because Pandas, per default, adds integer row and column indices 0, 1, 2, …. Again, think of them as the row and column indices in your Excel spreadsheet. You don’t want them to appear in the CSV file so you set the arguments to <code>False</code>.</li>
<li>You set the and <code>header</code> argument to <code>True</code> because you want the dict <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dict-keys-method/" data-type="post" data-id="37711" target="_blank">keys</a> to be used as headers of the CSV. </li>
</ul>
<p>If you want to customize the CSV output, you’ve got a lot of special arguments to play with. Check out <a rel="noreferrer noopener" href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_csv.html" target="_blank">this Finxter Tutorial</a> for a comprehensive list of all arguments.</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Related article</strong>:<a rel="noreferrer noopener" href="https://blog.finxter.com/pandas-cheat-sheets/" target="_blank"> Pandas Cheat Sheets to Pin to Your Wall</a></p>
<h2>Method 2: Dict to CSV File</h2>
<p class="has-global-color-8-background-color has-background">In Python, convert a dictionary to a CSV file <strong>using the <code>DictWriter()</code> method</strong> from the <code>csv</code> module. The <code>csv.DictWriter()</code> method allows you to insert data into the CSV file using its <code>DictWriter.writerow()</code> method. </p>
<p>The following example writes the dictionary to a CSV using the keys as column names and the values as row values.</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 data = {'A':'X1', 'B':'X2', 'C':'X3'} with open('my_file.csv', 'w', newline='') as f: writer = csv.DictWriter(f, fieldnames=data.keys()) writer.writeheader() writer.writerow(data)</pre>
<p>The resulting file <code>'my_file.csv'</code> looks like this:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="575" height="431" src="https://blog.finxter.com/wp-content/uploads/2022/08/image-30.png" alt="" class="wp-image-570490" srcset="https://blog.finxter.com/wp-content/uploads/2022/08/image-30.png 575w, https://blog.finxter.com/wp-content/uplo...00x225.png 300w" sizes="(max-width: 575px) 100vw, 575px" /></figure>
</div>
<p>The <code><a rel="noreferrer noopener" href="https://docs.python.org/3/library/csv.html" data-type="URL" data-id="https://docs.python.org/3/library/csv.html" target="_blank">csv</a></code> library may not yet installed on your machine. To check if it is installed, follow <a href="https://blog.finxter.com/how-to-check-csv-package-version-in-python/" data-type="post" data-id="479230">these instructions</a>. If it is not installed, fix it by running <code>pip install csv</code> in your shell or terminal.</p>
</p>
<h2>Method 3: Dict to CSV String (in Memory)</h2>
<p class="has-global-color-8-background-color has-background">To convert a list of dicts to a CSV string in memory, i.e., returning a CSV string instead of writing in a CSV file, use the <code>pandas.DataFrame.to_csv()</code> function without file path argument. The return value is a CSV string representation of the dictionary.</p>
<p>Here’s an example:</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 pandas as pd data = [{'A':'X1', 'B':'X2', 'C':'X3'}, {'A':'Y1', 'B':'Y2', 'C':'Y3'}] df = pd.DataFrame(data)
my_csv_string = df.to_csv(index=False) print(my_csv_string) '''
A,B,C
X1,X2,X3
Y1,Y2,Y3 '''</pre>
<p>We pass <code>index=False</code> because we don’t want an index 0, 1, 2 in front of each row.</p>
<h2>Method 4: Dict to CSV Append Line</h2>
<p class="has-global-color-8-background-color has-background">To append a dictionary to an existing CSV, you can <a href="https://blog.finxter.com/python-open-function/" data-type="post" data-id="24793" target="_blank" rel="noreferrer noopener">open</a> the file object in <a href="https://blog.finxter.com/append-to-a-file-python/" data-type="post" data-id="206350" target="_blank" rel="noreferrer noopener">append mode</a> using <code>open('my_file.csv', 'a', newline='')</code> and using the <code>csv.DictWriter()</code> to append a dict row using <code>DictWriter.writerow(my_dict)</code>.</p>
<p>Given the following file <code>'my_file.csv'</code>:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="573" height="432" src="https://blog.finxter.com/wp-content/uploads/2022/08/image-31.png" alt="" class="wp-image-570620" srcset="https://blog.finxter.com/wp-content/uploads/2022/08/image-31.png 573w, https://blog.finxter.com/wp-content/uplo...00x226.png 300w" sizes="(max-width: 573px) 100vw, 573px" /></figure>
</div>
<p>You can append a row (dict) to the CSV file via this code snippet:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="6,8" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import csv row = {'A':'Y1', 'B':'Y2', 'C':'Y3'} with open('my_file.csv', 'a', newline='') as f: writer = csv.DictWriter(f, fieldnames=row.keys()) writer.writerow(row)
</pre>
<p>After running the code in the same folder as your original <code>'my_file.csv'</code>, you’ll see the following result:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="576" height="430" src="https://blog.finxter.com/wp-content/uploads/2022/08/image-32.png" alt="" class="wp-image-570628" srcset="https://blog.finxter.com/wp-content/uploads/2022/08/image-32.png 576w, https://blog.finxter.com/wp-content/uplo...00x224.png 300w" sizes="(max-width: 576px) 100vw, 576px" /></figure>
</div>
<h2>Method 5: Dict to CSV Columns</h2>
<p>To write a Python dictionary in a CSV file as a column, i.e., a single <em>(key, value)</em> pair per row, use the following three steps:</p>
<ul class="has-global-color-8-background-color has-background">
<li>Open the file in writing mode and using the <code>newline=''</code> argument to prevent blank lines.</li>
<li>Create a CSV writer object.</li>
<li>Iterate over the (key, value) pairs of the dictionary using the <code><a href="https://blog.finxter.com/python-dict-items-method/" data-type="post" data-id="37673" target="_blank" rel="noreferrer noopener">dict.items()</a></code> method. </li>
<li>Write one <code>(key, value)</code> tuple at a time by passing it in the <code>writer.writerow()</code> method. </li>
</ul>
<p>Here’s the code example:</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 data = {'A':42, 'B':41, 'C':40} with open('my_file.csv', 'w', newline='') as f: writer = csv.writer(f) for row in data.items(): writer.writerow(row)</pre>
<p>Your output CSV file (column dict) looks like this:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="576" height="436" src="https://blog.finxter.com/wp-content/uploads/2022/08/image-33.png" alt="" class="wp-image-570658" srcset="https://blog.finxter.com/wp-content/uploads/2022/08/image-33.png 576w, https://blog.finxter.com/wp-content/uplo...00x227.png 300w" sizes="(max-width: 576px) 100vw, 576px" /></figure>
</div>
<h2>Method 6: Dict to CSV with Header</h2>
<p class="has-global-color-8-background-color has-background">Convert a Python dictionary to a CSV file with header <strong>using the <code>csv.DictWriter(fileobject, fieldnames)</code> method</strong> to create a writer object used for writing the header via <code>writer.writeheader()</code> without argument. This writes the list of column names passed as <code>fieldnames</code>, e.g., the dictionary keys obtained via <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-dict-keys-method/" data-type="post" data-id="37711" target="_blank">dict.keys()</a></code>.</p>
<p>To write the rows, you can then call the <code>DictWriter.writerow()</code> method. </p>
<p>The following example writes the dictionary to a CSV using the keys as column names and the values as row values.</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 data = {'A':'X1', 'B':'X2', 'C':'X3'} with open('my_file.csv', 'w', newline='') as f: writer = csv.DictWriter(f, fieldnames=data.keys()) writer.writeheader() writer.writerow(data)</pre>
<p>The resulting file <code>'my_file.csv'</code> looks like this:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="575" height="431" src="https://blog.finxter.com/wp-content/uploads/2022/08/image-30.png" alt="" class="wp-image-570490" srcset="https://blog.finxter.com/wp-content/uploads/2022/08/image-30.png 575w, https://blog.finxter.com/wp-content/uplo...00x225.png 300w" sizes="(max-width: 575px) 100vw, 575px" /></figure>
</div>
<p>Where to Go From Here</p>
<p>If you haven’t found your solution, yet, you may want to check out my in-depth guide on how to write a list of dicts to a CSV:</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Guide</strong>: <a href="https://blog.finxter.com/how-to-convert-a-list-of-dicts-to-a-csv-file-in-python/" data-type="post" data-id="557892">Write List of Dicts to CSV in Python</a></p>
</p>
<h2>Bonus Method 7: Vanilla Python</h2>
<p>If you don’t want to import any library and still convert a list of dicts into a CSV file, you can use standard Python implementation as well: it’s not complicated and very efficient.</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Finxter Recommended</strong>: <a rel="noreferrer noopener" href="https://blog.finxter.com/subscribe/" target="_blank">Join the Finxter community and download your 8+ Python cheat sheets to refresh your code understanding.</a></p>
<p><strong>This method is best if you won’t or cannot use external dependencies.</strong></p>
<ul class="has-global-color-8-background-color has-background">
<li>Open the file <code>f</code> in writing mode using the standard <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.</li>
<li>Write the first dictionary’s keys in the file using the <a rel="noreferrer noopener" href="https://pythononeliners.com/" data-type="URL" data-id="https://pythononeliners.com/" target="_blank">one-liner</a> expression <code>f.write(','.join(salary[0].keys()))</code>.</li>
<li>Iterate over the <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 dicts</a> and write the values in the CSV using the expression <code>f.write(','.join(str(x) for x in row.values()))</code>.</li>
</ul>
<p>Here’s the concrete code example:</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="">salary = [{'Name':'Alice', 'Job':'Data Scientist', 'Salary':122000}, {'Name':'Bob', 'Job':'Engineer', 'Salary':77000}, {'Name':'Carl', 'Job':'Manager', 'Salary':119000}] # Method 3
with open('my_file.csv','w') as f: f.write(','.join(salary[0].keys())) f.write('n') for row in salary: f.write(','.join(str(x) for x in row.values())) f.write('n')</pre>
<p>Output:</p>
<pre class="wp-block-preformatted"><code>Name,Job,Salary
Alice,Data Scientist,122000
Bob,Engineer,77000
Carl,Manager,119000</code></pre>
<p>In the code, you first open the file object <code>f</code>. Then you iterate over each row and each element in the row and write the element to the file—one by one. After each element, you place the comma to generate the CSV file format. After each row, you place the newline character <code>'n'</code>. </p>
<p><strong>Note</strong>: to get rid of the trailing comma, you can check if the element <code>x</code> is the last element in the row within the loop body and skip writing the comma if it is.</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f30d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Related Tutorial</strong>: <a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-convert-a-list-of-lists-to-a-csv-file-in-python/" data-type="URL" data-id="https://blog.finxter.com/how-to-convert-a-list-of-lists-to-a-csv-file-in-python/" target="_blank">How to Convert a List to a CSV File in Python [5 Ways]</a></p>
<h2>Programming Humor – Python</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="518" height="588" src="https://blog.finxter.com/wp-content/uploads/2022/07/image-65.png" alt="" class="wp-image-471102" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/image-65.png 518w, https://blog.finxter.com/wp-content/uplo...64x300.png 264w" sizes="(max-width: 518px) 100vw, 518px" /><figcaption><em>“I wrote 20 short programs in Python yesterday. It was wonderful. Perl, I’m leaving you.”</em> — <a rel="noreferrer noopener" href="https://imgs.xkcd.com/comics/python.png" data-type="URL" data-id="https://imgs.xkcd.com/comics/python.png" target="_blank">xkcd</a></figcaption></figure>
</div>
<h2>Where to Go From Here?</h2>
<p>Enough theory. Let’s get some practice!</p>
<p>Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation. </p>
<p>To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>You build high-value coding skills by working on practical coding projects!</strong></p>
<p>Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?</p>
<p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f680.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> If your answer is <strong><em>YES!</em></strong>, consider becoming a <a rel="noreferrer noopener" href="https://blog.finxter.com/become-python-freelancer-course/" data-type="page" data-id="2072" target="_blank">Python freelance developer</a>! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>If you just want to learn about the freelancing opportunity, feel free to watch my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and learn how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p></p>
</div>


https://www.sickgaming.net/blog/2022/08/...in-python/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016