Reading CSV Data into Custom Objects
When working with CSV files in Python, it is often desirable to read the data into custom objects rather than working with raw data structures like lists or dictionaries. This approach allows you to encapsulate the data and associated logic within your own classes, making the code more organized, maintainable, and easier to work with.
To read CSV data into custom objects, you can use the built-in csv
module in Python, along with the concept of data classes (introduced in Python 3.7) or regular classes.
Using Data Classes
Python's data classes provide a convenient way to define custom objects and automatically generate boilerplate code, such as __init__()
, __repr__()
, and __eq__()
methods. Here's an example of how to use data classes to read CSV data:
from dataclasses import dataclass
import csv
@dataclass
class Person:
name: str
age: int
city: str
with open('people.csv', 'r') as file:
reader = csv.DictReader(file)
people = [Person(**row) for row in reader]
for person in people:
print(person)
In this example, the Person
class is defined using the @dataclass
decorator, which automatically generates the necessary methods. The csv.DictReader
is used to read the CSV data into a dictionary, and then each row is used to create a Person
object.
Using Regular Classes
Alternatively, you can use regular Python classes to achieve the same result:
import csv
class Person:
def __init__(self, name, age, city):
self.name = name
self.age = age
self.city = city
def __repr__(self):
return f"Person(name='{self.name}', age={self.age}, city='{self.city}')"
with open('people.csv', 'r') as file:
reader = csv.reader(file)
next(reader) ## Skip the header row
people = [Person(*row) for row in reader]
for person in people:
print(person)
In this example, the Person
class is defined manually, with an __init__()
method to initialize the object's attributes and a __repr__()
method to provide a string representation of the object.
Both approaches allow you to work with the CSV data in a more structured and object-oriented manner, making it easier to manage and manipulate the data within your Python application.