Sick Gaming
[Tut] The Ultimate Guide on Converting a CSV 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] The Ultimate Guide on Converting a CSV in Python (/thread-99741.html)



[Tut] The Ultimate Guide on Converting a CSV in Python - xSicKxBot - 07-25-2022

The Ultimate Guide on Converting a 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;503412&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&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;4&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;4\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;113.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: 113.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;"> 4/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/1f40d.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Abstract</strong>: In this article, we’ll quickly overview the best method, respectively, to convert a CSV file to JSON, Excel, dictionary, Parquet, list, list of lists, list of tuples, text file, DataFrame, XML, NumPy array, and list of dictionaries.</p>
<p>In this article, you’ve learned the best ways to perform the following conversions (click to read more):</p>
<ul>
<li><a href="https://blog.finxter.com/python-convert-csv-to-_-8-different-target-formats/#How_to_Convert_CSV_to_JSON_in_Python">How to Convert CSV to JSON in Python?</a></li>
<li><a href="https://blog.finxter.com/python-convert-csv-to-_-8-different-target-formats/#How_to_Convert_CSV_to_Excel_XLSX_in_Python">How to Convert CSV to Excel (XLSX) in Python?</a></li>
<li><a href="https://blog.finxter.com/python-convert-csv-to-_-8-different-target-formats/#How_to_Convert_a_CSV_to_a_Dictionary_in_Python">How to Convert a CSV to a Dictionary in Python?</a></li>
<li><a href="https://blog.finxter.com/python-convert-csv-to-_-8-different-target-formats/#How_to_Convert_a_CSV_to_a_Parquet_Format_in_Python">How to Convert a CSV to a Parquet Format in Python?</a></li>
<li><a href="https://blog.finxter.com/python-convert-csv-to-_-8-different-target-formats/#How_to_Convert_a_CSV_to_a_List_in_Python">How to Convert a CSV to a List in Python?</a></li>
<li><a href="https://blog.finxter.com/python-convert-csv-to-_-8-different-target-formats/#How_to_Convert_a_CSV_to_a_List_of_Lists_in_Python">How to Convert a CSV to a List of Lists in Python?</a></li>
<li><a href="https://blog.finxter.com/python-convert-csv-to-_-8-different-target-formats/#How_to_Convert_a_CSV_to_a_List_of_Tuples_in_Python">How to Convert a CSV to a List of Tuples in Python?</a></li>
<li><a href="https://blog.finxter.com/python-convert-csv-to-_-8-different-target-formats/#How_to_Convert_a_CSV_to_a_Text_File_in_Python">How to Convert a CSV to a Text File in Python?</a></li>
<li><a href="https://blog.finxter.com/python-convert-csv-to-_-8-different-target-formats/#How_to_Convert_a_CSV_to_a_Pandas_DataFrame_in_Python">How to Convert a CSV to a Pandas DataFrame in Python?</a></li>
<li><a href="https://blog.finxter.com/python-convert-csv-to-_-8-different-target-formats/#How_to_Convert_a_CSV_to_an_XML_in_Python">How to Convert a CSV to an XML in Python?</a></li>
<li><a href="https://blog.finxter.com/python-convert-csv-to-_-8-different-target-formats/#How_to_Convert_a_CSV_to_a_NumPy_Array_in_Python">How to Convert a CSV to a NumPy Array in Python?</a></li>
<li><a href="https://blog.finxter.com/python-convert-csv-to-_-8-different-target-formats/#How_to_Convert_a_CSV_to_a_List_of_Dictionaries">How to Convert a CSV to a List of Dictionaries?</a></li>
</ul>
<h2>How to Convert CSV to JSON in Python?</h2>
<p>You can convert a CSV file to a JSON file by using the following five steps:</p>
<ol class="has-global-color-8-background-color has-background">
<li>Import the <code>csv</code> and <code>json</code> libraries</li>
<li>Open the CSV as a file object in reading mode using the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-open-function/" target="_blank">open(path_to_csv, 'r')</a></code> function in a context manager (=<code>with</code> environment).</li>
<li>Load the CSV content into Python using the <code>csv.DictReader(fobj)</code> and pass the file object just created.</li>
<li>Iterate over each <code>row</code> and update a newly-created <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" target="_blank">dictionary</a> <code>my_json</code> using one of the column values as key: <code>my_json[key] = row</code></li>
<li>Store the <code>my_json</code> dictionary data in a JSON file using the <code><a href="https://blog.finxter.com/how-to-serialize-a-python-dict-into-a-string-and-back/" target="_blank" rel="noreferrer noopener">json.dumps(my_json)</a></code> function.</li>
</ol>
<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
import json csv_file = 'my_file.csv'
json_file = 'my_file.json' my_json = {}
with open(csv_file, 'r') as fobj: reader = csv.DictReader(fobj) for row in reader: # Use one of the CSV column names as a key key = row['Name'] my_json[key] = row with open(json_file,'w') as fobj: fobj.write(json.dumps(my_json, indent=2))</pre>
<p><strong>Input CSV File:</strong></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,Job,Age,Income
Alice,Programmer,23,110000
Bob,Executive,34,90000
Carl,Sales,45,50000</pre>
<p><strong>Output JSON File:</strong></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="">{ "Alice": { "Name": "Alice", "Job": "Programmer", "Age": "23", "Income": "110000" }, "Bob": { "Name": "Bob", "Job": "Executive", "Age": "34", "Income": "90000" }, "Carl": { "Name": "Carl", "Job": "Sales", "Age": "45", "Income": "50000" }
}</pre>
<p>There are many more details to it, so if this didn’t answer your question yet, go here:</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>: Feel free to learn more about this conversion goal in our <a href="https://blog.finxter.com/convert-csv-to-json-in-python/" data-type="URL" data-id="https://blog.finxter.com/convert-csv-to-json-in-python/" target="_blank" rel="noreferrer noopener">full guide on the Finxter blog</a> with multiple CSV conversion methods and step-by-step explanations.</p>
<h2>How to Convert CSV to Excel (XLSX) in Python?</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="1024" height="576" src="https://blog.finxter.com/wp-content/uploads/2022/07/image-81.png" alt="" class="wp-image-503484" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/image-81.png 1024w, https://blog.finxter.com/wp-content/uploads/2022/07/image-81-300x169.png 300w, https://blog.finxter.com/wp-content/uploads/2022/07/image-81-768x432.png 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>The most pythonic way to convert a <code>.csv</code> to an <code>.xlsx</code> (Excel) in Python is to use the <a href="https://blog.finxter.com/pandas-quickstart/" target="_blank" rel="noreferrer noopener">Pandas</a> library.</p>
<ol class="has-global-color-8-background-color has-background">
<li>Install the <code>pandas</code> library with <code><a href="https://blog.finxter.com/how-to-install-pandas-in-python/" target="_blank" rel="noreferrer noopener">pip install pandas</a></code></li>
<li>Install the <code>openpyxl</code> library that is used internally by pandas with <code><a href="https://blog.finxter.com/fixed-modulenotfounderror-no-module-named-openpyxl/" target="_blank" rel="noreferrer noopener">pip install openpyxl</a></code></li>
<li>Import the <code>pandas</code> libray with <code>import pandas as pd</code></li>
<li>Read the <a href="https://blog.finxter.com/read-and-write-flat-files-with-pandas/" target="_blank" rel="noreferrer noopener">CSV file into a DataFrame</a> <code>df</code> by using the expression <code>df = pd.read_csv('my_file.csv')</code></li>
<li>Store the <a href="https://blog.finxter.com/pandas-dataframe-to_excel-method/" target="_blank" rel="noreferrer noopener">DataFrame in an Excel</a> file by calling <code>df.to_excel('my_file.xlsx', index=None, header=True)</code></li>
</ol>
<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 pandas as pd df = pd.read_csv('my_file.csv')
df.to_excel('my_file.xlsx', index=None, header=True)</pre>
<p>Note that there are many ways to customize the <code><a href="https://blog.finxter.com/pandas-dataframe-to_excel-method/" data-type="post" data-id="344278">to_</a><a href="https://blog.finxter.com/pandas-dataframe-to_excel-method/" data-type="post" data-id="344278" target="_blank" rel="noreferrer noopener">e</a><a href="https://blog.finxter.com/pandas-dataframe-to_excel-method/" data-type="post" data-id="344278">xcel()</a></code> function in case</p>
<ul>
<li>you don’t need a header line,</li>
<li>you want to fix the first line in the Excel file,</li>
<li>you want to format the cells as numbers instead of strings, or</li>
<li>you have an index column in the original CSV and want to consider it in the Excel file too.</li>
</ul>
<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>: Feel free to learn more about this conversion goal in our <a href="https://blog.finxter.com/convert-csv-to-excel-xlsx-in-python/" data-type="URL" data-id="https://blog.finxter.com/convert-csv-to-excel-xlsx-in-python/" target="_blank" rel="noreferrer noopener">full guide on the Finxter blog</a> with multiple CSV conversion methods and step-by-step explanations.</p>
<h2>How to Convert a CSV to a Dictionary in Python?</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="1024" height="576" src="https://blog.finxter.com/wp-content/uploads/2022/07/image-82.png" alt="" class="wp-image-503494" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/image-82.png 1024w, https://blog.finxter.com/wp-content/uploads/2022/07/image-82-300x169.png 300w, https://blog.finxter.com/wp-content/uploads/2022/07/image-82-768x432.png 768w" 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>
<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="">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 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>: Feel free to learn more about this conversion goal in our <a href="https://blog.finxter.com/convert-csv-to-dictionary-in-python/" data-type="URL" data-id="https://blog.finxter.com/convert-csv-to-dictionary-in-python/" target="_blank" rel="noreferrer noopener">full guide on the Finxter blog</a> with multiple CSV conversion methods and step-by-step explanations.</p>
<h2>How to Convert a CSV to a Parquet Format in Python?</h2>
<p>Here’s a step-by-step approach to reading a CSV and converting its contents to a Parquet file using the <a href="https://blog.finxter.com/pandas-quickstart/" target="_blank" rel="noreferrer noopener">Pandas library</a>:</p>
<ul class="has-global-color-8-background-color has-background">
<li><strong>Step 1</strong>: Run <code><a href="https://blog.finxter.com/how-to-install-pandas-in-python/" target="_blank" rel="noreferrer noopener">pip install pandas</a></code> if the module is not already installed in your environment.</li>
<li><strong>Step 2</strong>: Run <code>pip install pyarrow</code> to <a href="https://blog.finxter.com/how-to-install-pyarrow-in-python/" target="_blank" rel="noreferrer noopener">install <code>pyarrow</code></a> module</li>
<li><strong>Step 3</strong>: Run <code>pip install fastparquet</code> to install the <code>fastparquet</code> module</li>
<li><strong>Step 4</strong>: import pandas using <code>import pandas as pd</code></li>
<li><strong>Step 5</strong>: <a href="https://blog.finxter.com/read-and-write-flat-files-with-pandas/" target="_blank" rel="noreferrer noopener">Read the CSV</a> file into a DataFrame using <code>df = pd.read_csv('my_file.csv')</code>.</li>
<li><strong>Step 6</strong>: Write the <a href="https://blog.finxter.com/pandas-dataframe-to_parquet-method/" target="_blank" rel="noreferrer noopener">Parquet</a> file using <code>df.to_parquet('my_file.parquet')</code></li>
</ul>
<p>The code snippet to convert a CSV file to a Parquet file is quite simple (steps 4-6):</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 pandas as pd
df = pd.read_csv('my_file.csv')
df.to_parquet('my_file.parquet')</pre>
<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>: Feel free to learn more about this conversion goal in our <a href="https://blog.finxter.com/python-convert-csv-to-parquet/" data-type="URL" data-id="https://blog.finxter.com/python-convert-csv-to-parquet/" target="_blank" rel="noreferrer noopener">full guide on the Finxter blog</a> with multiple CSV conversion methods and step-by-step explanations.</p>
<h2>How to Convert a CSV to a List in Python?</h2>
<figure class="wp-block-image"><img loading="lazy" width="1024" height="576" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-166-1024x576.png" alt="" class="wp-image-437780" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-166-1024x576.png 1024w, https://blog.finxter.com/wp-content/uploads/2022/06/image-166-300x169.png 300w, https://blog.finxter.com/wp-content/uploads/2022/06/image-166-768x432.png 768w, https://blog.finxter.com/wp-content/uploads/2022/06/image-166.png 1280w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<p>Here’s the code to convert that CSV file to a list of dictionaries, one <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" 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="" 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) lst = list(*reader)</pre>
<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>: Feel free to learn more about this conversion goal in our <a href="https://blog.finxter.com/python-convert-csv-to-list/" data-type="URL" data-id="https://blog.finxter.com/python-convert-csv-to-list/" target="_blank" rel="noreferrer noopener">full guide on the Finxter blog</a> with multiple CSV conversion methods and step-by-step explanations.</p>
<h2>How to Convert a CSV to a List of Lists in Python?</h2>
<figure class="wp-block-image size-full"><img loading="lazy" width="1024" height="576" src="https://blog.finxter.com/wp-content/uploads/2022/07/image-83.png" alt="" class="wp-image-503518" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/image-83.png 1024w, https://blog.finxter.com/wp-content/uploads/2022/07/image-83-300x169.png 300w, https://blog.finxter.com/wp-content/uploads/2022/07/image-83-768x432.png 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<p class="has-global-color-8-background-color has-background">To convert a CSV file <code>'my_file.csv'</code> into a list of lists in Python, use the <code>csv.reader(file_obj)</code> method to create a CSV file reader. Then convert the resulting object to a list using the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-list/" target="_blank">list()</a></code> constructor.</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 csv_filename = 'my_file.csv' with open(csv_filename) as f: reader = csv.reader(f) lst = list(reader)</pre>
<p>Output:</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="">print(lst)
# [['9', '8', '7'], ['6', '5', '4'], ['3', '2', '1']]</pre>
<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>: Feel free to learn more about this conversion goal in our <a href="https://blog.finxter.com/python-convert-csv-to-list-of-lists/" data-type="URL" data-id="https://blog.finxter.com/python-convert-csv-to-list-of-lists/" target="_blank" rel="noreferrer noopener">full guide on the Finxter blog</a> with multiple CSV conversion methods and step-by-step explanations.</p>
<h2>How to Convert a CSV to a List of Tuples in Python?</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img loading="lazy" width="1024" height="576" src="https://blog.finxter.com/wp-content/uploads/2022/07/image-84.png" alt="" class="wp-image-503524" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/image-84.png 1024w, https://blog.finxter.com/wp-content/uploads/2022/07/image-84-300x169.png 300w, https://blog.finxter.com/wp-content/uploads/2022/07/image-84-768x432.png 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p class="has-global-color-8-background-color has-background">To convert a CSV file <code>'my_file.csv'</code> into a list of tuples in Python, use <code>csv.reader(file_obj)</code> to create a CSV file reader that holds an iterable of lists, one per row. Now, use the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-list/" target="_blank"><code>list(tuple(line) for line in reader)</code></a> expression with a <a rel="noreferrer noopener" href="https://blog.finxter.com/understanding-generators-in-python/" target="_blank">generator expression</a> to convert each inner list to a tuple.</p>
<p>Here’s a simple example that converts our CSV file to a list of tuples using this approach:</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 csv_filename = 'my_file.csv' with open(csv_filename) as f: reader = csv.reader(f) lst = list(tuple(line) for line in reader)</pre>
<p>Output:</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="">print(lst)
# [('9', '8', '7'), ('6', '5', '4'), ('3', '2', '1')]</pre>
<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>: Feel free to learn more about this conversion goal in our <a href="https://blog.finxter.com/convert-csv-to-list-of-tuples-in-python/" data-type="URL" data-id="https://blog.finxter.com/convert-csv-to-list-of-tuples-in-python/" target="_blank" rel="noreferrer noopener">full guide on the Finxter blog</a> with multiple CSV conversion methods and step-by-step explanations.</p>
<h2>How to Convert a CSV to a Text File in Python?</h2>
<p class="has-global-color-8-background-color has-background">If you want to keep the content (including the delimiter <code>','</code>) in the CSV file unmodified, the conversion is simple: read the <code>.csv</code> file and write its content into a new <code>.txt</code> file using the <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-open-function/" target="_blank">open()</a></code>, <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-read-binary-file/" target="_blank">read()</a></code>, and <code><a rel="noreferrer noopener" href="https://blog.finxter.com/python-one-liner-write-string-to-file/" target="_blank">write()</a></code> functions without importing any library.</p>
<p>In other words, perform the three steps to write a CSV to a TXT file unmodified:</p>
<ol>
<li>Open the CSV file in reading mode and the TXT file in writing mode.</li>
<li>Read the CSV file and store it in a variable.</li>
<li>Write the content into the TXT file.</li>
</ol>
<p>Here’s the code snippet that solves our basic challenge:</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=""># 1. Open the CSV file in reading mode and the TXT file in writing mode
with open('my_file.csv', 'r') as f_in, open('my_file.txt', 'w') as f_out: # 2. Read the CSV file and store in variable content = f_in.read() # 3. Write the content into the TXT file f_out.write(content)</pre>
<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>: Feel free to learn more about this conversion goal in our <a href="https://blog.finxter.com/python-convert-csv-to-text-file-csv-to-txt/" data-type="URL" data-id="https://blog.finxter.com/python-convert-csv-to-text-file-csv-to-txt/" target="_blank" rel="noreferrer noopener">full guide on the Finxter blog</a> with multiple CSV conversion methods and step-by-step explanations.</p>
<h2>How to Convert a CSV to a Pandas DataFrame in Python?</h2>
<p>To import a given CSV file into a newly-created Pandas DataFrame, use the <code>pd.read_csv('my_file.csv')</code> function that returns a DataFrame created with the content in the CSV file <code>'my_file.csv'</code>.</p>
<p>Here’s a quick and generic code snippet showcasing this approach:</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 pandas as pd
df = pd.read_csv('my_file.csv')
print(df)</pre>
<p>Output:</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 Job Age Income
0 Alice Programmer 23 110000
1 Bob Executive 34 90000
2 Carl Sales 45 50000</pre>
<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>: Feel free to learn more about this conversion goal in our <a href="https://blog.finxter.com/read-a-csv-file-to-a-pandas-dataframe/" data-type="URL" data-id="https://blog.finxter.com/read-a-csv-file-to-a-pandas-dataframe/" target="_blank" rel="noreferrer noopener">full guide on the Finxter blog</a> with multiple CSV conversion methods and step-by-step explanations.</p>
<h2>How to Convert a CSV to an XML in Python?</h2>
<figure class="wp-block-image size-full"><img loading="lazy" width="1024" height="576" src="https://blog.finxter.com/wp-content/uploads/2022/07/image-85.png" alt="" class="wp-image-503546" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/image-85.png 1024w, https://blog.finxter.com/wp-content/uploads/2022/07/image-85-300x169.png 300w, https://blog.finxter.com/wp-content/uploads/2022/07/image-85-768x432.png 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<p>You can convert a CSV to an XML using the following approach:</p>
<ul class="has-global-color-8-background-color has-background">
<li>Read the whole CSV file into your Python script.</li>
<li>Store the first row as header data that is needed to name your custom XML tags (e.g., <code>&lt;Name&gt;</code>, <code>&lt;Job&gt;</code>, <code>&lt;Age&gt;</code>, and <code>&lt;Income&gt;</code> in our example).</li>
<li>Create a function <code>convert_row()</code> that converts each row separately to an XML representation of that row using <a rel="noreferrer noopener" href="https://blog.finxter.com/string-formatting-vs-format-vs-formatted-string-literal/" target="_blank">basic string formatting</a>.</li>
<li>Iterate over the data row-wise using <code><a rel="noreferrer noopener" href="https://blog.finxter.com/how-to-read-a-csv-file-into-a-python-list/" target="_blank">csv.reader()</a></code> and convert each CSV row to XML using your function <code>convert_row()</code>.</li>
</ul>
<p>Here’s the code:</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=""># Convert CSV file to XML string
import csv filename = 'my_file.csv' def convert_row(headers, row): s = f'&lt;row id="{row[0]}">\n' for header, item in zip(headers, row): s += f' &lt;{header}>' + f'{item}' + f'&lt;/{header}>\n' return s + '&lt;/row>' with open(filename, 'r') as f: r = csv.reader(f) headers = next® xml = '&lt;data>\n' for row in r: xml += convert_row(headers, row) + '\n' xml += '&lt;/data>' print(xml)</pre>
<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>: Feel free to learn more about this conversion goal in our <a href="https://blog.finxter.com/csv-to-xml-how-to-convert-in-python/" data-type="URL" data-id="https://blog.finxter.com/csv-to-xml-how-to-convert-in-python/" target="_blank" rel="noreferrer noopener">full guide on the Finxter blog</a> with multiple CSV conversion methods and step-by-step explanations.</p>
<h2>How to Convert a CSV to a NumPy Array in Python?</h2>
<figure class="wp-block-image size-full"><img loading="lazy" width="1024" height="576" src="https://blog.finxter.com/wp-content/uploads/2022/07/image-86.png" alt="" class="wp-image-503551" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/image-86.png 1024w, https://blog.finxter.com/wp-content/uploads/2022/07/image-86-300x169.png 300w, https://blog.finxter.com/wp-content/uploads/2022/07/image-86-768x432.png 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<p class="has-global-color-8-background-color has-background">You can convert a CSV file to a NumPy array simply by calling <code><a rel="noreferrer noopener" href="https://numpy.org/doc/stable/reference/generated/numpy.loadtxt.html" target="_blank">np.loadtxt()</a></code> with two arguments: the <code>filename</code> and the <code>delimiter</code> string. For example, the expression <code>np.loadtxt('my_file.csv', delimiter=',')</code> returns a NumPy array from the <code>'my_file.csv'</code> with delimiter symbols <code>','</code>.</p>
<p>Here’s an 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 numpy as np array = np.loadtxt('my_file.csv', delimiter=',')
print(array)</pre>
<p>Output:</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="">[[9. 8. 7.] [6. 5. 4.] [3. 2. 1.]]</pre>
<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>: Feel free to learn more about this conversion goal in our <a href="https://blog.finxter.com/how-to-convert-a-csv-to-numpy-array-in-python/" data-type="URL" data-id="https://blog.finxter.com/how-to-convert-a-csv-to-numpy-array-in-python/" target="_blank" rel="noreferrer noopener">full guide on the Finxter blog</a> with multiple CSV conversion methods and step-by-step explanations.</p>
<h2>How to Convert a CSV to a List of Dictionaries?</h2>
<figure class="wp-block-image size-full"><img loading="lazy" width="1024" height="576" src="https://blog.finxter.com/wp-content/uploads/2022/07/image-87.png" alt="" class="wp-image-503561" srcset="https://blog.finxter.com/wp-content/uploads/2022/07/image-87.png 1024w, https://blog.finxter.com/wp-content/uploads/2022/07/image-87-300x169.png 300w, https://blog.finxter.com/wp-content/uploads/2022/07/image-87-768x432.png 768w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<p>Convert a CSV file to a list of Python dictionaries in three steps:</p>
<ol class="has-global-color-8-background-color has-background">
<li>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.</li>
<li>The return value is an iterable of dictionaries, one per row in the CSV file. Each dictionary maps the column header from the first row to the specific row value.</li>
<li>As the last step, convert the iterable of dictionaries to a list using the Python built-in <code>list()</code> function.</li>
</ol>
<p>Here’s the code to convert that CSV file to a list of dictionaries, one <a rel="noreferrer noopener" href="https://blog.finxter.com/python-dictionary/" 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="" 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) lst = list(*reader)</pre>
<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>: Feel free to learn more about this conversion goal in our <a href="https://blog.finxter.com/python-convert-csv-to-list-of-dictionaries/" data-type="URL" data-id="https://blog.finxter.com/python-convert-csv-to-list-of-dictionaries/" target="_blank" rel="noreferrer noopener">full guide on the Finxter blog</a> with multiple CSV conversion methods and step-by-step explanations.</p>
<h2>Summary</h2>
<p>You can find a more detailed article on each topic in the following table:</p>
<figure class="wp-block-table is-style-stripes">
<table>
<thead>
<tr>
<th>Goal Conversion</th>
<th>Full Article</th>
</tr>
</thead>
<tbody>
<tr>
<td>Python CSV to JSON</td>
<td><a href="https://blog.finxter.com/convert-csv-to-json-in-python/" target="_blank" rel="noreferrer noopener">https://blog.finxter.com/convert-csv-to-json-in-python/</a></td>
</tr>
<tr>
<td>Python CSV to Excel (<code>.xlsx</code>)</td>
<td><a href="https://blog.finxter.com/convert-csv-to-excel-xlsx-in-python/" target="_blank" rel="noreferrer noopener">https://blog.finxter.com/convert-csv-to-excel-xlsx-in-python/</a></td>
</tr>
<tr>
<td>Python CSV to Dictionary</td>
<td><a href="https://blog.finxter.com/convert-csv-to-dictionary-in-python/" target="_blank" rel="noreferrer noopener">https://blog.finxter.com/convert-csv-to-dictionary-in-python/</a></td>
</tr>
<tr>
<td>Python CSV to Parquet</td>
<td><a href="https://blog.finxter.com/python-convert-csv-to-parquet/" target="_blank" rel="noreferrer noopener">https://blog.finxter.com/python-convert-csv-to-parquet/</a></td>
</tr>
<tr>
<td>Python CSV to List</td>
<td><a href="https://blog.finxter.com/python-convert-csv-to-list/" target="_blank" rel="noreferrer noopener">https://blog.finxter.com/python-convert-csv-to-list/</a></td>
</tr>
<tr>
<td>Python CSV to List of Lists</td>
<td><a href="https://blog.finxter.com/python-convert-csv-to-list-of-lists/" target="_blank" rel="noreferrer noopener">https://blog.finxter.com/python-convert-csv-to-list-of-lists/</a></td>
</tr>
<tr>
<td>Python CSV to List of Tuples</td>
<td><a href="https://blog.finxter.com/convert-csv-to-list-of-tuples-in-python/" target="_blank" rel="noreferrer noopener">https://blog.finxter.com/convert-csv-to-list-of-tuples-in-python/</a></td>
</tr>
<tr>
<td>Python CSV to Text File (<code>.txt</code>)</td>
<td><a href="https://blog.finxter.com/python-convert-csv-to-text-file-csv-to-txt/" target="_blank" rel="noreferrer noopener">https://blog.finxter.com/python-convert-csv-to-text-file-csv-to-txt/</a></td>
</tr>
<tr>
<td>Python CSV to DataFrame (Pandas)</td>
<td><a href="https://blog.finxter.com/read-a-csv-file-to-a-pandas-dataframe/" target="_blank" rel="noreferrer noopener">https://blog.finxter.com/read-a-csv-file-to-a-pandas-dataframe/</a></td>
</tr>
<tr>
<td>Python CSV to XML File (<code>.xml</code>)</td>
<td><a href="https://blog.finxter.com/csv-to-xml-how-to-convert-in-python/" target="_blank" rel="noreferrer noopener">https://blog.finxter.com/csv-to-xml-how-to-convert-in-python/</a></td>
</tr>
<tr>
<td>Python CSV to NumPy Array (<code>ndarray</code>)</td>
<td><a href="https://blog.finxter.com/how-to-convert-a-csv-to-numpy-array-in-python/" target="_blank" rel="noreferrer noopener">https://blog.finxter.com/how-to-convert-a-csv-to-numpy-array-in-python/</a></td>
</tr>
<tr>
<td>Python CSV to List of Dictionaries</td>
<td><a href="https://blog.finxter.com/python-convert-csv-to-list-of-dictionaries/" target="_blank" rel="noreferrer noopener">https://blog.finxter.com/python-convert-csv-to-list-of-dictionaries/</a></td>
</tr>
</tbody>
</table>
</figure>
<p>Feel free to check out the <a href="https://blog.finxter.com/email-academy/" data-type="page" data-id="12278" target="_blank" rel="noreferrer noopener">Finxter email academy</a> to keep improving your coding skills. We have cheat sheets!</p>
<h2>Programmer Humor</h2>
<div class="wp-block-image">
<figure class="aligncenter size-full is-resized"><a href="https://imgs.xkcd.com/comics/computers_vs_humans.png" target="_blank" rel="noreferrer noopener"><img loading="lazy" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-163.png" alt="" class="wp-image-435467" width="578" height="282" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-163.png 578w, https://blog.finxter.com/wp-content/uploads/2022/06/image-163-300x146.png 300w" sizes="(max-width: 578px) 100vw, 578px" /></a><figcaption><em>It’s hard to train deep learning algorithms when most of the positive feedback they get is sarcastic.</em> — from <a href="https://imgs.xkcd.com/comics/computers_vs_humans.png" data-type="URL" data-id="https://imgs.xkcd.com/comics/computers_vs_humans.png" target="_blank" rel="noreferrer noopener">xkcd</a></figcaption></figure>
</div>
</div>


https://www.sickgaming.net/blog/2022/07/22/the-ultimate-guide-on-converting-a-csv-in-python/