![]() |
|
[Tut] How to Convert a List of Objects to a CSV File in Python [5 Ways] - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Convert a List of Objects to a CSV File in Python [5 Ways] (/thread-99753.html) |
[Tut] How to Convert a List of Objects to a CSV File in Python [5 Ways] - xSicKxBot - 07-30-2022 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 Code: csvExample: Given is a list of custom objects of, say, type Code: EmployeeCode: 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: Code: # my_file.csv
Alice,Data Scientist,122000
Bob,Engineer,77000
Ann,Manager,119000Solution: There are four simple ways to convert a list of lists to a CSV file in Python.
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 Code: csvCode: 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: Code: # my_file.csv
Alice,Data Scientist,122000
Bob,Engineer,77000
Ann,Manager,119000In the code, you first open the file using Python’s standard Code: open()Code: fNext, 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 Code: writerow()You now iterate over the objects and convert each object to a list. The list representing one row is then passed in the Code: writerow()You can customize the CSV writer in its constructor (e.g., by modifying the delimiter from a comma Code: ','Code: ' 'Method 2: Pandas DataFrame to_csv() This method converts a list of objects to a CSV file in two steps:
![]() You can convert a list of lists to aPandas DataFrame that provides you with powerful capabilities such as the Code: to_csv()This is a super simple approach that avoids importing yet another library (I use Pandas in many Python projects anyways). Code: 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: Code: # my_file.csv
Alice,Data Scientist,122000
Bob,Engineer,77000
Ann,Manager,119000Code Main Steps:
You convert a list of objects to a CSV file in three main steps.
You set the Code: indexCode: headerCode: to_csv()Code: FalseThink of them as the row and column indices in your Excel spreadsheet. You don’t want them to appear in the CSV file so you set the arguments to Code: FalseIf you want to customize the CSV output, you’ve got a lot of special arguments to play with. Check out thisarticle for a comprehensive list of all arguments. ![]() Related article:Pandas Cheat Sheets to Pin to Your WallMethod 3: NumPy savetext() NumPyis 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 Code: savetext()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. Code: 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 Code: hash()Code: nameCode: descriptionCode: EmployeeOutput: Code: # 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+05The output doesn’t look pretty: it stores the values as floats. But no worries, you can reformat the output using the format argument Code: fmtCode: savetxt()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 Code: open()Code: file.write()This method is best if you won’t or cannot use external dependencies. Code: 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: Code: # my_file.csv
Alice,Data Scientist,122000,
Bob,Engineer,77000,
Ann,Manager,119000,In the code, you first open the file object Code: fWe use Python’s f-string functionality to do that in a concise way. At the end of each row, you place the newline character Code: '\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. ![]() Code: # 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-Linerswill 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:
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!! |