{"id":126801,"date":"2022-07-25T14:05:59","date_gmt":"2022-07-25T14:05:59","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=510694"},"modified":"2022-07-25T14:05:59","modified_gmt":"2022-07-25T14:05:59","slug":"how-to-convert-a-list-of-objects-to-a-csv-file-in-python-5-ways","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2022\/07\/25\/how-to-convert-a-list-of-objects-to-a-csv-file-in-python-5-ways\/","title":{"rendered":"How to Convert a List of Objects to a CSV File in Python [5 Ways]"},"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;510694&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;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;}\">\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/div>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f4ac.png\" alt=\"\ud83d\udcac\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Question<\/strong>: How to convert a list of custom objects to a <code>csv<\/code> file? <\/p>\n<p><strong>Example<\/strong>: Given is a list of custom objects of, say, type <code>Employee<\/code> that holds the name, job description, and income like so:<\/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=\"\">salary = [Employee('Alice', 'Data Scientist', 122000), Employee('Bob', 'Engineer', 77000), Employee('Ann', 'Manager', 119000)]<\/pre>\n<p>Your goal is to write the content of the list of objects into a <a rel=\"noreferrer noopener\" href=\"https:\/\/en.wikipedia.org\/wiki\/Comma-separated_values\" target=\"_blank\">comma-separated-values<\/a> (CSV) file format. <\/p>\n<p>Your output file should look like this:<\/p>\n<pre class=\"wp-block-preformatted\"><code><strong><em># my_file.csv<\/em><\/strong>\nAlice,Data Scientist,122000\nBob,Engineer,77000\nAnn,Manager,119000<\/code><\/pre>\n<p><strong>Solution: <\/strong>There are four simple ways to convert a <a href=\"https:\/\/blog.finxter.com\/python-lists\/\" target=\"_blank\" rel=\"noreferrer noopener\">list<\/a> of lists to a CSV file in <a href=\"https:\/\/blog.finxter.com\/python-cheat-sheets\/\" target=\"_blank\" rel=\"noreferrer noopener\">Python<\/a>.<\/p>\n<ol>\n<li><strong>CSV<\/strong>: Import the <code>csv<\/code> <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/csv.html\" target=\"_blank\">module<\/a> in Python, create a csv writer object, and find a list <code>lst<\/code> of elements representing each object as a row, that is then written into the CSV using <code>writer.writerow(lst)<\/code>.<\/li>\n<li><strong>Pandas<\/strong>: Import the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/pandas-cheat-sheets\/\" target=\"_blank\">pandas library<\/a>, convert each object to a list to obtain a list of lists, create a Pandas DataFrame out of the list of lists, and write the DataFrame to a file using the DataFrame method <code>DataFrame.to_csv('file.csv')<\/code>.<\/li>\n<li><strong>NumPy<\/strong>: Import the <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/collection-10-best-numpy-cheat-sheets-every-python-coder-must-own\/\" target=\"_blank\">NumPy library<\/a>, convert each object to a list to obtain a list of lists, create a NumPy array, and write the output to a CSV file using the <code>numpy.savetxt('file.csv', array, delimiter=',')<\/code> method.<\/li>\n<li><strong>Python<\/strong>: Use a pure <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/python-crash-course\/\" target=\"_blank\">Python<\/a> implementation that doesn&#8217;t require any library by using the Python file I\/O functionality.<\/li>\n<\/ol>\n<p class=\"has-global-color-8-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/2b50.png\" alt=\"\u2b50\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <strong>Finxter Favorite<\/strong>: My preference is Method 4 (<strong>Vanilla Python<\/strong>) because it&#8217;s simplest to use, efficient, and most robust for different input types (numerical or textual) and doesn&#8217;t require external dependencies and data wrangling.<\/p>\n<h2>Method 1: Python&#8217;s CSV Module<\/h2>\n<p>You can convert a list of lists to a CSV file in Python easily&#8212;by using the <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/csv.html\" target=\"_blank\"><code>csv<\/code><\/a> library. <strong>This is the most customizable of all four methods.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"13-18\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Employee(object): def __init__(self, name, description, salary): self.name = name self.description = description self.salary = salary employees = [Employee('Alice', 'Data Scientist', 122000), Employee('Bob', 'Engineer', 77000), Employee('Ann', 'Manager', 119000)] # Method 1\nimport csv\nwith open('my_file.csv', 'w', newline='') as f: writer = csv.writer(f) for x in employees: writer.writerow([x.name, x.description, x.salary]) <\/pre>\n<p>Output:<\/p>\n<pre class=\"wp-block-preformatted\"><code><strong><em># my_file.csv<\/em><\/strong>\nAlice,Data Scientist,122000\nBob,Engineer,77000\nAnn,Manager,119000<\/code><\/pre>\n<p>In the code, you first open the file using Python&#8217;s standard <code><a href=\"https:\/\/blog.finxter.com\/python-open-function\/\" data-type=\"post\" data-id=\"24793\" target=\"_blank\" rel=\"noreferrer noopener\">open()<\/a><\/code> command. Now, you can write content to the file object <code>f<\/code>. <\/p>\n<p>Next, you pass this file object to the constructor of the CSV writer that implements some additional helper method&#8212;and effectively wraps the file object providing you with new CSV-specific functionality such as the <code>writerow()<\/code> method. <\/p>\n<p>You now iterate over the objects and convert each object to a list.<\/p>\n<p>The list representing one row is then passed in the <code>writerow()<\/code> method of the CSV writer. This takes care of converting the list of objects to a CSV format. <\/p>\n<p>You can customize the CSV writer in its constructor (e.g., by modifying the delimiter from a comma <code>','<\/code> to a whitespace <code>' '<\/code> character). Have a look at the specification to learn about <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.python.org\/3\/library\/csv.html\" target=\"_blank\">advanced modifications<\/a>. <\/p>\n<h2>Method 2: Pandas DataFrame to_csv()<\/h2>\n<p>This method converts a list of objects to a CSV file in two steps:<\/p>\n<ul>\n<li>First, convert the list of objects to a <a href=\"https:\/\/blog.finxter.com\/python-list-of-lists\/\" data-type=\"post\" data-id=\"7890\" target=\"_blank\" rel=\"noreferrer noopener\">list of lists<\/a>.<\/li>\n<li>Second, <a href=\"https:\/\/blog.finxter.com\/how-to-convert-a-list-of-lists-to-a-csv-file-in-python\/\" data-type=\"post\" data-id=\"7999\">convert the list of lists to a CSV<\/a> (e.g., using pandas <code>to_csv()<\/code>).<\/li>\n<\/ul>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/04\/graphic-1024x576.jpg\" alt=\"List of Lists to CSV\" class=\"wp-image-8024\" width=\"512\" height=\"288\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/04\/graphic-scaled.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/04\/graphic-300x169.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/04\/graphic-768x432.jpg 768w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/figure>\n<\/div>\n<p>You can convert a list of lists 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 <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>. <\/p>\n<p><strong>This is a super simple approach that avoids importing yet another library<\/strong> (I use Pandas in many Python projects anyways). <\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"13-21\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Employee(object): def __init__(self, name, description, salary): self.name = name self.description = description self.salary = salary employees = [Employee('Alice', 'Data Scientist', 122000), Employee('Bob', 'Engineer', 77000), Employee('Ann', 'Manager', 119000)] # Method 2\nimport pandas as pd # Step 1: Convert list of objects to list of lists\nlst = [[x.name, x.description, x.salary] for x in employees] # Step 2: Convert list of lists to CSV\ndf = pd.DataFrame(lst)\ndf.to_csv('my_file.csv', index=False, header=False) <\/pre>\n<p>Output:<\/p>\n<pre class=\"wp-block-preformatted\"><code><strong><em># my_file.csv<\/em><\/strong>\nAlice,Data Scientist,122000\nBob,Engineer,77000\nAnn,Manager,119000<\/code><\/pre>\n<p><strong>Code Main Steps:<\/strong><\/p>\n<ol>\n<li><code>lst = [[x.name, x.description, x.salary] for x in employees]<\/code><\/li>\n<li><code>df = pd.DataFrame(lst)<\/code><\/li>\n<li><code>df.to_csv('my_file.csv', index=False, header=False)<\/code><\/li>\n<\/ol>\n<p>You convert a list of objects to a CSV file in three main steps.<\/p>\n<ol>\n<li>First, convert the list of objects to a list of lists by using <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/list-comprehension\/\" data-type=\"post\" data-id=\"1171\" target=\"_blank\">list comprehension<\/a> to iterate over each object and convert each object to an inner list using your custom expression.<\/li>\n<li>Second, 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>, Python&#8217;s default representation of tabular data. <\/li>\n<li>Third, the DataFrame is a very powerful data structure that allows you to perform various methods. One of those is the <code><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/pandas-dataframe-to_csv-method\/\" data-type=\"post\" data-id=\"344277\" target=\"_blank\">to_csv()<\/a><\/code> method that allows you to write its contents into a CSV file.<\/li>\n<\/ol>\n<p>You set the <code>index<\/code> and <code>header<\/code> arguments of the <code>to_csv()<\/code> method to <code>False<\/code> because Pandas, per default, adds integer row and column indices 0, 1, 2, &#8230;. <\/p>\n<p>Think of them as the row and column indices in your Excel spreadsheet. You don&#8217;t want them to appear in the CSV file so you set the arguments to <code>False<\/code>. <\/p>\n<p>If you want to customize the CSV output, you&#8217;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 <\/a>article for a comprehensive list of all arguments.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><a href=\"https:\/\/blog.finxter.com\/pandas-cheat-sheets\/\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" loading=\"lazy\" width=\"741\" height=\"573\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/04\/image-257.png\" alt=\"\" class=\"wp-image-329079\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/04\/image-257.png 741w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2022\/04\/image-257-300x232.png 300w\" sizes=\"auto, (max-width: 741px) 100vw, 741px\" \/><\/a><\/figure>\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\/1f30d.png\" alt=\"\ud83c\udf0d\" 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>\n<h2>Method 3: NumPy savetext()<\/h2>\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/numpy-tutorial\/\" target=\"_blank\">NumPy <\/a>is at the core of Python&#8217;s <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/freelance-data-science\/\" data-type=\"post\" data-id=\"16658\" target=\"_blank\">data science<\/a> and <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/machine-learning-engineer-income-and-opportunity\/\" data-type=\"post\" data-id=\"306050\" target=\"_blank\">machine learning<\/a> functionality. Even <a href=\"https:\/\/blog.finxter.com\/pandas-quickstart\/\" data-type=\"post\" data-id=\"16511\" target=\"_blank\" rel=\"noreferrer noopener\">Pandas<\/a> uses NumPy arrays to implement critical functionality.<\/p>\n<p class=\"has-global-color-8-background-color has-background\">You can convert a list of objects to a CSV file by first converting it to a list of lists which is then converted to a NumPy array, and then using <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.scipy.org\/doc\/numpy\/reference\/generated\/numpy.savetxt.html\" target=\"_blank\">NumPy&#8217;s <code>savetext()<\/code> function<\/a> by passing the NumPy array as an argument.<\/p>\n<p><strong>This method is best if you can represent the numerical data only&#8212;otherwise, it&#8217;ll lead to complicated data type conversions which are not recommended.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"13-23\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Employee(object): def __init__(self, name, description, salary): self.name = name self.description = description self.salary = salary employees = [Employee('Alice', 'Data Scientist', 122000), Employee('Bob', 'Engineer', 77000), Employee('Ann', 'Manager', 119000)] # Method 3\nimport numpy as np # Convert list of objects to list of lists\nlst = [[hash(x.name), hash(x.description), x.salary] for x in employees] # Convert list of lists to NumPy array\na = np.array(lst) # Convert array to CSV\nnp.savetxt('my_file.csv', a, delimiter=',') <\/pre>\n<p>In the code, we use the <code><a href=\"https:\/\/blog.finxter.com\/python-hash-function\/\" data-type=\"post\" data-id=\"24483\" target=\"_blank\" rel=\"noreferrer noopener\">hash()<\/a><\/code> function to obtain a numerical value for the string attributes <code>name<\/code> and <code>description<\/code> of the <code>Employee<\/code> class.<\/p>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-preformatted\"><code><strong><em># my_file.csv<\/em><\/strong><\/code>\n<code>-8.655249391637094400e+18,-4.821993523891147776e+18,1.220000000000000000e+05\n7.826671284149683200e+18,-7.040934892515148800e+18,7.700000000000000000e+04\n3.577554885237667328e+18,1.887669837421876992e+18,1.190000000000000000e+05<\/code><\/pre>\n<p>The output doesn&#8217;t look pretty: it stores the values as floats. But no worries, you can reformat the output using the format argument <code>fmt<\/code> of the <code>savetxt()<\/code> method (<a rel=\"noreferrer noopener\" href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.savetxt.html?highlight=save#numpy.savetxt\" target=\"_blank\">more here<\/a>). However, I&#8217;d recommend you stick to method 2 (Pandas) to avoid unnecessary complexity in your code.<\/p>\n<h2>Method 4: Pure Python Without External Dependencies<\/h2>\n<p>If you don&#8217;t want to import any library and still convert a list of objects into a CSV file, you can use standard Python implementation as well: it&#8217;s not complicated but very efficient.<\/p>\n<p class=\"has-global-color-8-background-color has-background\">The idea is simple, iterate over the list of object and write a comma-separated representation of each object into the CSV file using a combination of the built-in <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 to create a file object and the <code>file.write()<\/code> method to write each row.<\/p>\n<p><strong>This method is best if you won&#8217;t or cannot use external dependencies.<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"13-16\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">class Employee(object): def __init__(self, name, description, salary): self.name = name self.description = description self.salary = salary employees = [Employee('Alice', 'Data Scientist', 122000), Employee('Bob', 'Engineer', 77000), Employee('Ann', 'Manager', 119000)] # Method 4\nwith open('my_file.csv', 'w') as f: for x in employees: f.write(f'{x.name},{x.description},{x.salary}\\n') <\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"wp-block-preformatted\"><code><strong><em># my_file.csv<\/em><\/strong>\nAlice,Data Scientist,122000,\nBob,Engineer,77000,\nAnn,Manager,119000,<\/code><\/pre>\n<p>In the code, you first open the file object <code>f<\/code>. Then you iterate over each object and write a custom comma-separated string representation of this object to the file using the file.write() method. <\/p>\n<p>We use Python&#8217;s <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/string-formatting-vs-format-vs-formatted-string-literal\/\" data-type=\"post\" data-id=\"13190\" target=\"_blank\">f-string<\/a> functionality to do that in a concise way. At the end of each row, you place the newline character <code>'\\n'<\/code>. <\/p>\n<h2>Method 5 &#8211; Bonus: Python One-Liner<\/h2>\n<p>The previous method is a <a href=\"https:\/\/blog.finxter.com\/python-one-liners\/\" data-type=\"post\" data-id=\"13555\" target=\"_blank\" rel=\"noreferrer noopener\">one-linerized<\/a> variant of <strong>Method 4<\/strong>. If you&#8217;re part of the Finxter community, you know how I love one-liners. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f609.png\" alt=\"\ud83d\ude09\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"2\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\"># Method 5\nopen('my_file.csv', 'w').writelines([f'{x.name},{x.description},{x.salary}\\n' for x in employees])<\/pre>\n<p>Concise, isn&#8217;t it? The output is the same as before.<\/p>\n<p>If you&#8217;re interested in the art of crafting beautiful one-liners, check out my book on the topic!<\/p>\n<h2>Python One-Liners Book: Master the Single Line First!<\/h2>\n<p><strong>Python programmers will improve their computer science skills with these useful one-liners.<\/strong><\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-medium is-resized\"><a href=\"https:\/\/www.amazon.com\/gp\/product\/B07ZY7XMX8\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/3D_cover-1024x944.jpg\" alt=\"Python One-Liners\" class=\"wp-image-10007\" width=\"512\" height=\"472\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/3D_cover-scaled.jpg 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/3D_cover-300x277.jpg 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2020\/06\/3D_cover-768x708.jpg 768w\" sizes=\"auto, (max-width: 512px) 100vw, 512px\" \/><\/a><\/figure>\n<\/div>\n<p><a href=\"https:\/\/amzn.to\/2WAYeJE\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/amzn.to\/2WAYeJE\"><em>Python One-Liners<\/em> <\/a>will teach you how to read and write &#8220;one-liners&#8221;: <strong><em>concise statements of useful functionality packed into a single line of code. <\/em><\/strong>You&#8217;ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.<\/p>\n<p>The book&#8217;s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms. <\/p>\n<p>Detailed explanations of one-liners introduce <strong><em>key computer science concepts <\/em><\/strong>and<strong><em> boost your coding and analytical skills<\/em><\/strong>. You&#8217;ll learn about advanced Python features such as <em><strong>list comprehension<\/strong><\/em>, <strong><em>slicing<\/em><\/strong>, <strong><em>lambda functions<\/em><\/strong>, <strong><em>regular expressions<\/em><\/strong>, <strong><em>map <\/em><\/strong>and <strong><em>reduce <\/em><\/strong>functions, and <strong><em>slice assignments<\/em><\/strong>. <\/p>\n<p>You&#8217;ll also learn how to:<\/p>\n<ul>\n<li>Leverage data structures to <strong>solve real-world problems<\/strong>, like using Boolean indexing to find cities with above-average pollution<\/li>\n<li>Use <strong>NumPy basics<\/strong> such as <em>array<\/em>, <em>shape<\/em>, <em>axis<\/em>, <em>type<\/em>, <em>broadcasting<\/em>, <em>advanced indexing<\/em>, <em>slicing<\/em>, <em>sorting<\/em>, <em>searching<\/em>, <em>aggregating<\/em>, and <em>statistics<\/em><\/li>\n<li>Calculate basic <strong>statistics <\/strong>of multidimensional data arrays and the K-Means algorithms for unsupervised learning<\/li>\n<li>Create more <strong>advanced regular expressions<\/strong> using <em>grouping <\/em>and <em>named groups<\/em>, <em>negative lookaheads<\/em>, <em>escaped characters<\/em>, <em>whitespaces, character sets<\/em> (and <em>negative characters sets<\/em>), and <em>greedy\/nongreedy operators<\/em><\/li>\n<li>Understand a wide range of <strong>computer science topics<\/strong>, including <em>anagrams<\/em>, <em>palindromes<\/em>, <em>supersets<\/em>, <em>permutations<\/em>, <em>factorials<\/em>, <em>prime numbers<\/em>, <em>Fibonacci <\/em>numbers, <em>obfuscation<\/em>, <em>searching<\/em>, and <em>algorithmic sorting<\/em><\/li>\n<\/ul>\n<p>By the end of the book, you&#8217;ll know how to <strong><em>write Python at its most refined<\/em><\/strong>, and create concise, beautiful pieces of &#8220;Python art&#8221; in merely a single line.<\/p>\n<p><strong><a href=\"https:\/\/amzn.to\/2WAYeJE\" target=\"_blank\" rel=\"noreferrer noopener\" title=\"https:\/\/amzn.to\/2WAYeJE\"><em>Get your Python One-Liners on Amazon!!<\/em><\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) Question: How to convert a list of custom objects to a csv file? Example: Given is a list of custom objects of, say, type Employee that holds the name, job description, and income like so: salary = [Employee(&#8216;Alice&#8217;, &#8216;Data Scientist&#8217;, 122000), Employee(&#8216;Bob&#8217;, &#8216;Engineer&#8217;, 77000), Employee(&#8216;Ann&#8217;, &#8216;Manager&#8217;, 119000)] Your goal is to [&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-126801","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\/126801","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=126801"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/126801\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=126801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=126801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=126801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}