Convert a CSV File to a List of Dictionaries in Python

To convert a CSV file into a list of dictionaries in Python, we use the csv.DictReader() function from the csv module. This function reads each row of the CSV file and returns a dictionary where the column headers act as keys, and the row values become corresponding values. This method is useful for structured data representation and easy manipulation.


Examples to Convert CSV to a List of Dictionaries

1. Using csv.DictReader() to Convert CSV to List of Dictionaries

In this example, we will read a CSV file and convert each row into a dictionary using the csv.DictReader() function.

Sample CSV File (employees.csv):

main.py

</>
Copy
import csv

# Define the file path
file_path = "employees.csv"

# Open and read the CSV file
with open(file_path, mode="r", newline="") as file:
    csv_reader = csv.DictReader(file)
    
    # Convert CSV data into a list of dictionaries
    data_list = [row for row in csv_reader]

# Print the result
print(data_list)

Explanation:

  1. import csv: Imports the csv module to handle CSV files.
  2. file_path: Defines the path of the CSV file.
  3. with open(file_path, mode="r", newline="") as file: Opens the CSV file in read mode.
  4. csv.DictReader(file): Reads the CSV file and returns each row as a dictionary.
  5. data_list = [row for row in csv_reader]: Converts all rows into a list of dictionaries.
  6. print(data_list): Displays the list of dictionaries.

Output:

2. Handling CSV File with Different Delimiters

Some CSV files use delimiters other than commas (e.g., semicolons). We can specify the delimiter while reading the file.

Sample CSV File (data_semicolon.csv):

main.py

</>
Copy
import csv

# Define the file path
file_path = "students.csv"

# Open and read the CSV file with a different delimiter
with open(file_path, mode="r", newline="") as file:
    csv_reader = csv.DictReader(file, delimiter=";")
    
    # Convert CSV data into a list of dictionaries
    data_list = [row for row in csv_reader]

# Print the result
print(data_list)

Explanation:

  1. The CSV file uses a semicolon (;) as a delimiter instead of a comma.
  2. csv.DictReader(file, delimiter=";"): Specifies the delimiter to correctly read the data.
  3. All other steps remain the same as the previous example.

Output:

3. Converting CSV File with Integer Values

By default, all values in the dictionary are strings. We can convert numerical values to integers.

Sample CSV File (numeric_data.csv):

id,name,score
1,Alice,85
2,Bob,92
3,Charlie,78

main.py

</>
Copy
import csv

# Define the file path
file_path = "numeric_data.csv"

# Open and read the CSV file
with open(file_path, mode="r", newline="") as file:
    csv_reader = csv.DictReader(file)
    
    # Convert CSV data into a list of dictionaries with integer conversion
    data_list = [{key: int(value) if value.isdigit() else value for key, value in row.items()} for row in csv_reader]

# Print the result
print(data_list)

Explanation:

  1. value.isdigit(): Checks if the value is a numeric string.
  2. int(value) if value.isdigit() else value: Converts numeric strings to integers.
  3. All other steps remain the same as in previous examples.

Output:

[
    {'id': 1, 'name': 'Alice', 'score': 85},
    {'id': 2, 'name': 'Bob', 'score': 92},
    {'id': 3, 'name': 'Charlie', 'score': 78}
]

Conclusion

In this tutorial, we learned different ways to convert a CSV file into a list of dictionaries using Python’s csv.DictReader() function. We also handled different delimiters and converted numerical values correctly.