How to Convert a List of Objects to a CSV File in Python [5 Ways]

5/5 - (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('Alice', 'Data Scientist', 122000),
          Employee('Bob', 'Engineer', 77000),
          Employee('Ann', 'Manager', 119000)]

Your goal is to write the content of the list of objects into a comma-separated-values (CSV) file format.

Your output file should look like this:

# my_file.csv
Alice,Data Scientist,122000
Bob,Engineer,77000
Ann,Manager,119000

Solution: There are four simple ways to convert a list of lists to a CSV file in Python.

  1. CSV: Import the csv module in Python, create a csv writer object, and find a list lst of elements representing each object as a row, that is then written into the CSV using writer.writerow(lst).
  2. Pandas: Import the pandas library, 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 DataFrame.to_csv('file.csv').
  3. NumPy: Import the NumPy library, 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 numpy.savetxt('file.csv', array, delimiter=',') method.
  4. Python: Use a pure Python implementation that doesn’t require any library by using the Python file I/O functionality.

⭐ Finxter Favorite: My preference is Method 4 (Vanilla Python) because it’s simplest to use, efficient, and most robust for different input types (numerical or textual) and doesn’t require external dependencies and data wrangling.

Method 1: Python’s CSV Module

You can convert a list of lists to a CSV file in Python easily—by using the csv library. This is the most customizable of all four methods.

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
import csv
with open('my_file.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    for x in employees:
        writer.writerow([x.name, x.description, x.salary])
        

Output:

# my_file.csv
Alice,Data Scientist,122000
Bob,Engineer,77000
Ann,Manager,119000

In the code, you first open the file using Python’s standard open() command. Now, you can write content to the file object f.

Next, you pass this file object to the constructor of the CSV writer that implements some additional helper method—and effectively wraps the file object providing you with new CSV-specific functionality such as the writerow() method.

You now iterate over the objects and convert each object to a list.

The list representing one row is then passed in the writerow() method of the CSV writer. This takes care of converting the list of objects to a CSV format.

You can customize the CSV writer in its constructor (e.g., by modifying the delimiter from a comma ',' to a whitespace ' ' character). Have a look at the specification to learn about advanced modifications.

Method 2: Pandas DataFrame to_csv()

This method converts a list of objects to a CSV file in two steps:

List of Lists to CSV

You can convert a list of lists to a Pandas DataFrame that provides you with powerful capabilities such as the to_csv() method.

This is a super simple approach that avoids importing yet another library (I use Pandas in many Python projects anyways).

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
import pandas as pd

# Step 1: Convert list of objects to list of lists
lst = [[x.name, x.description, x.salary] for x in employees]

# Step 2: Convert list of lists to CSV
df = pd.DataFrame(lst)
df.to_csv('my_file.csv', index=False, header=False)

Output:

# my_file.csv
Alice,Data Scientist,122000
Bob,Engineer,77000
Ann,Manager,119000

Code Main Steps:

  1. lst = [[x.name, x.description, x.salary] for x in employees]
  2. df = pd.DataFrame(lst)
  3. df.to_csv('my_file.csv', index=False, header=False)

You convert a list of objects to a CSV file in three main steps.

  1. First, convert the list of objects to a list of lists by using list comprehension to iterate over each object and convert each object to an inner list using your custom expression.
  2. Second, create a Pandas DataFrame, Python’s default representation of tabular data.
  3. Third, the DataFrame is a very powerful data structure that allows you to perform various methods. One of those is the to_csv() method that allows you to write its contents into a CSV file.

You set the index and header arguments of the to_csv() method to False because Pandas, per default, adds integer row and column indices 0, 1, 2, ….

Think of them as the row and column indices in your Excel spreadsheet. You don’t want them to appear in the CSV file so you set the arguments to False.

If you want to customize the CSV output, you’ve got a lot of special arguments to play with. Check out this article for a comprehensive list of all arguments.

🌍 Related article: Pandas Cheat Sheets to Pin to Your Wall

Method 3: NumPy savetext()

NumPy is at the core of Python’s data science and machine learning functionality. Even Pandas uses NumPy arrays to implement critical functionality.

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 NumPy’s savetext() function by passing the NumPy array as an argument.

This method is best if you can represent the numerical data only—otherwise, it’ll lead to complicated data type conversions which are not recommended.

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
import numpy as np

# Convert list of objects to list of lists
lst = [[hash(x.name), hash(x.description), x.salary] for x in employees]

# Convert list of lists to NumPy array
a = np.array(lst)

# Convert array to CSV
np.savetxt('my_file.csv', a, delimiter=',')

In the code, we use the hash() function to obtain a numerical value for the string attributes name and description of the Employee class.

Output:

# my_file.csv
-8.655249391637094400e+18,-4.821993523891147776e+18,1.220000000000000000e+05
7.826671284149683200e+18,-7.040934892515148800e+18,7.700000000000000000e+04
3.577554885237667328e+18,1.887669837421876992e+18,1.190000000000000000e+05

The output doesn’t look pretty: it stores the values as floats. But no worries, you can reformat the output using the format argument fmt of the savetxt() method (more here). However, I’d recommend you stick to method 2 (Pandas) to avoid unnecessary complexity in your code.

Method 4: Pure Python Without External Dependencies

If you don’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’s not complicated but very efficient.

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 open() function to create a file object and the file.write() method to write each row.

This method is best if you won’t or cannot use external dependencies.

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
with open('my_file.csv', 'w') as f:
    for x in employees:
        f.write(f'{x.name},{x.description},{x.salary}\n')
        

Output:

# my_file.csv
Alice,Data Scientist,122000,
Bob,Engineer,77000,
Ann,Manager,119000,

In the code, you first open the file object f. 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.

We use Python’s f-string functionality to do that in a concise way. At the end of each row, you place the newline character '\n'.

Method 5 – Bonus: Python One-Liner

The previous method is a one-linerized variant of Method 4. If you’re part of the Finxter community, you know how I love one-liners. πŸ˜‰

# Method 5
open('my_file.csv', 'w').writelines([f'{x.name},{x.description},{x.salary}\n' for x in employees])

Concise, isn’t it? The output is the same as before.

If you’re interested in the art of crafting beautiful one-liners, check out my book on the topic!

Python One-Liners Book: Master the Single Line First!

Python programmers will improve their computer science skills with these useful one-liners.

Python One-Liners

Python One-Liners will teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.

The book’s five chapters cover (1) tips and tricks, (2) regular expressions, (3) machine learning, (4) core data science topics, and (5) useful algorithms.

Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments.

You’ll also learn how to:

  • Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution
  • Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics
  • Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning
  • Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators
  • Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting

By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.

Get your Python One-Liners on Amazon!!