{"id":126755,"date":"2022-07-22T14:59:15","date_gmt":"2022-07-22T14:59:15","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=503412"},"modified":"2022-07-22T14:59:15","modified_gmt":"2022-07-22T14:59:15","slug":"the-ultimate-guide-on-converting-a-csv-in-python","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/07\/22\/the-ultimate-guide-on-converting-a-csv-in-python\/","title":{"rendered":"The Ultimate Guide on Converting a CSV in Python"},"content":{"rendered":"\n<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;}\">\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: 113.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\" style=\"font-size: 19.2px;\"> 4\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f40d.png\" alt=\"\ud83d\udc0d\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Abstract<\/strong>: In this article, we&#8217;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>\n<p>In this article, you&#8217;ve learned the best ways to perform the following conversions (click to read more):<\/p>\n<ul>\n<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>\n<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>\n<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>\n<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>\n<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>\n<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>\n<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>\n<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>\n<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>\n<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>\n<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>\n<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>\n<\/ul>\n<h2>How to Convert CSV to JSON in Python?<\/h2>\n<p>You can convert a CSV file to a JSON file by using the following five steps:<\/p>\n<ol class=\"has-global-color-8-background-color has-background\">\n<li>Import the <code>csv<\/code> and <code>json<\/code> libraries<\/li>\n<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>\n<li>Load the CSV content into Python using the <code>csv.DictReader(fobj)<\/code> and pass the file object just created.<\/li>\n<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>\n<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>\n<\/ol>\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\nimport json csv_file = 'my_file.csv'\njson_file = 'my_file.json' my_json = {}\nwith 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>\n<p><strong>Input CSV File:<\/strong><\/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,Job,Age,Income\nAlice,Programmer,23,110000\nBob,Executive,34,90000\nCarl,Sales,45,50000<\/pre>\n<p><strong>Output JSON File:<\/strong><\/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=\"\">{ \"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\" }\n}<\/pre>\n<p>There are many more details to it, so if this didn&#8217;t answer your question yet, go here:<\/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\/1f30d.png\" alt=\"\ud83c\udf0d\" 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>\n<h2>How to Convert CSV to Excel (XLSX) in Python?<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" 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=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<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>\n<ol class=\"has-global-color-8-background-color has-background\">\n<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>\n<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>\n<li>Import the <code>pandas<\/code> libray with <code>import pandas as pd<\/code><\/li>\n<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>\n<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>\n<\/ol>\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 pandas as pd df = pd.read_csv('my_file.csv')\ndf.to_excel('my_file.xlsx', index=None, header=True)<\/pre>\n<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>\n<ul>\n<li>you don\u2019t need a header line,<\/li>\n<li>you want to fix the first line in the Excel file,<\/li>\n<li>you want to format the cells as numbers instead of strings, or<\/li>\n<li>you have an index column in the original CSV and want to consider it in the Excel file too.<\/li>\n<\/ul>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" 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>\n<h2>How to Convert a CSV to a Dictionary in Python?<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" 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=\"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<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>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" 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>\n<h2>How to Convert a CSV to a Parquet Format in Python?<\/h2>\n<p>Here\u2019s 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>\n<ul class=\"has-global-color-8-background-color has-background\">\n<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>\n<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>\n<li><strong>Step 3<\/strong>: Run <code>pip install fastparquet<\/code> to install the <code>fastparquet<\/code> module<\/li>\n<li><strong>Step 4<\/strong>: import pandas using <code>import pandas as pd<\/code><\/li>\n<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>\n<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>\n<\/ul>\n<p>The code snippet to convert a CSV file to a Parquet file is quite simple (steps 4-6):<\/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 pandas as pd\ndf = pd.read_csv('my_file.csv')\ndf.to_parquet('my_file.parquet')<\/pre>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" 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>\n<h2>How to Convert a CSV to a List in Python?<\/h2>\n<figure class=\"wp-block-image\"><img decoding=\"async\" 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=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<p>Here\u2019s 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>\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 csv_filename = 'my_file.csv' with open(csv_filename) as f: reader = csv.DictReader(f) lst = list(*reader)<\/pre>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" 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>\n<h2>How to Convert a CSV to a List of Lists in Python?<\/h2>\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" 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=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<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>\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 csv_filename = 'my_file.csv' with open(csv_filename) as f: reader = csv.reader(f) lst = list(reader)<\/pre>\n<p>Output:<\/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=\"\">print(lst)\n# [['9', '8', '7'], ['6', '5', '4'], ['3', '2', '1']]<\/pre>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" 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>\n<h2>How to Convert a CSV to a List of Tuples in Python?<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" 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=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<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>\n<p>Here\u2019s a simple example that converts our CSV file to a list of tuples using this approach:<\/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 csv_filename = 'my_file.csv' with open(csv_filename) as f: reader = csv.reader(f) lst = list(tuple(line) for line in reader)<\/pre>\n<p>Output:<\/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=\"\">print(lst)\n# [('9', '8', '7'), ('6', '5', '4'), ('3', '2', '1')]<\/pre>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" 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>\n<h2>How to Convert a CSV to a Text File in Python?<\/h2>\n<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>\n<p>In other words, perform the three steps to write a CSV to a TXT file unmodified:<\/p>\n<ol>\n<li>Open the CSV file in reading mode and the TXT file in writing mode.<\/li>\n<li>Read the CSV file and store it in a variable.<\/li>\n<li>Write the content into the TXT file.<\/li>\n<\/ol>\n<p>Here\u2019s the code snippet that solves our basic challenge:<\/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=\"\"># 1. Open the CSV file in reading mode and the TXT file in writing mode\nwith 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>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" 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>\n<h2>How to Convert a CSV to a Pandas DataFrame in Python?<\/h2>\n<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>\n<p>Here\u2019s a quick and generic code snippet showcasing this approach:<\/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 pandas as pd\ndf = pd.read_csv('my_file.csv')\nprint(df)<\/pre>\n<p>Output:<\/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 Job Age Income\n0 Alice Programmer 23 110000\n1 Bob Executive 34 90000\n2 Carl Sales 45 50000<\/pre>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" 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>\n<h2>How to Convert a CSV to an XML in Python?<\/h2>\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" 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=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<p>You can convert a CSV to an XML using the following approach:<\/p>\n<ul class=\"has-global-color-8-background-color has-background\">\n<li>Read the whole CSV file into your Python script.<\/li>\n<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>\n<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>\n<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>\n<\/ul>\n<p>Here&#8217;s the code:<\/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=\"\"># Convert CSV file to XML string\nimport 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(r) xml = '&lt;data>\\n' for row in r: xml += convert_row(headers, row) + '\\n' xml += '&lt;\/data>' print(xml)<\/pre>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" 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>\n<h2>How to Convert a CSV to a NumPy Array in Python?<\/h2>\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" 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=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<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>\n<p>Here&#8217;s an example:<\/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 numpy as np array = np.loadtxt('my_file.csv', delimiter=',')\nprint(array)<\/pre>\n<p>Output:<\/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=\"\">[[9. 8. 7.] [6. 5. 4.] [3. 2. 1.]]<\/pre>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" 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>\n<h2>How to Convert a CSV to a List of Dictionaries?<\/h2>\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" 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=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<p>Convert a CSV file to a list of Python dictionaries in three steps:<\/p>\n<ol class=\"has-global-color-8-background-color has-background\">\n<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>\n<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>\n<li>As the last step, convert the iterable of dictionaries to a list using the Python built-in <code>list()<\/code> function.<\/li>\n<\/ol>\n<p>Here\u2019s 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>\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 csv_filename = 'my_file.csv' with open(csv_filename) as f: reader = csv.DictReader(f) lst = list(*reader)<\/pre>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f30d.png\" alt=\"\ud83c\udf0d\" 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>\n<h2>Summary<\/h2>\n<p>You can find a more detailed article on each topic in the following table:<\/p>\n<figure class=\"wp-block-table is-style-stripes\">\n<table>\n<thead>\n<tr>\n<th>Goal Conversion<\/th>\n<th>Full Article<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Python CSV to JSON<\/td>\n<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>\n<\/tr>\n<tr>\n<td>Python CSV to Excel (<code>.xlsx<\/code>)<\/td>\n<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>\n<\/tr>\n<tr>\n<td>Python CSV to Dictionary<\/td>\n<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>\n<\/tr>\n<tr>\n<td>Python CSV to Parquet<\/td>\n<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>\n<\/tr>\n<tr>\n<td>Python CSV to List<\/td>\n<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>\n<\/tr>\n<tr>\n<td>Python CSV to List of Lists<\/td>\n<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>\n<\/tr>\n<tr>\n<td>Python CSV to List of Tuples<\/td>\n<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>\n<\/tr>\n<tr>\n<td>Python CSV to Text File (<code>.txt<\/code>)<\/td>\n<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>\n<\/tr>\n<tr>\n<td>Python CSV to DataFrame (Pandas)<\/td>\n<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>\n<\/tr>\n<tr>\n<td>Python CSV to XML File (<code>.xml<\/code>)<\/td>\n<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>\n<\/tr>\n<tr>\n<td>Python CSV to NumPy Array (<code>ndarray<\/code>)<\/td>\n<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>\n<\/tr>\n<tr>\n<td>Python CSV to List of Dictionaries<\/td>\n<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>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n<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>\n<h2>Programmer Humor<\/h2>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/imgs.xkcd.com\/comics\/computers_vs_humans.png\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" 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=\"auto, (max-width: 578px) 100vw, 578px\" \/><\/a><figcaption><em>It&#8217;s hard to train deep learning algorithms when most of the positive feedback they get is sarcastic.<\/em> &#8212; 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>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>4\/5 &#8211; (1 vote) Abstract: In this article, we&#8217;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. In this article, you&#8217;ve learned the best ways to perform the following conversions [&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-126755","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\/126755","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=126755"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/126755\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=126755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=126755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=126755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}