Home > other >  Using Python to convert CSV file into multiple dict
Using Python to convert CSV file into multiple dict

Time:10-19

I would like to convert CSV file into list of dicts. For example I have a CSV file which has a data in following order:

name,hobby,age

Sammy,football,6

Angela,chess,12

and the output should look like this:

[

  {"name": "Sammy", "hobby": "football", "age": "6"},

  {"name": "Angela", "hobby": "chess", "age": "12"}

]

Do you have any suggestions how to do it?

CodePudding user response:

If you're okay using Pandas, this can be done as -

import pandas as pd

df = pd.read_csv('/path/to/csv/file')
records = df.to_dict(orient='records')

The output should be like -

[
  {"name": "Sammy", "hobby": "football", "age": "6"},
  {"name": "Angela", "hobby": "chess", "age": "12"}
]

Here, we are reading the csv file as pandas DataFrame, and then converting the dataframe to dict. In case, pandas is not available, install using

pip install pandas

CodePudding user response:

You can use this code with only csv module:

import csv

with open(filename, mode='r') as infile:
    reader = csv.reader(infile, skipinitialspace=True)
    keys = next(reader)
    ret_list = []
    for row in reader:
        ret_list.append({})
        for key, value in zip(keys, row):
            ret_list[-1][key] = value

Update: This is more practical solution:

import csv

with open(filename, mode='r') as infile:
    reader = csv.DictReader(infile, skipinitialspace=True)
    d = [r for r in reader]

CodePudding user response:

here is a method to create a list of dictionaries;

import pandas as pd
# Replace './a.xlsx' with path to your file
# In case file is in csv use pd.read_csv instead
df = pd.read_excel('./a.xlsx')

# Create an empty list to hold the list of dictionaries
list_of_dicts = list()

# Use iterrows to iterate over all rows
for index, row in df.iterrows():

    # Empty dictionary to be used as tmp value for each dict in list
    dict_person = {}
    # Iterate over each column on the row, update the dictionary key and value
    for col in range(len(row.index)):
        dict_person.update({str(row.index[col]) : row[col]})
    # Add the temporary dict to the list
    list_of_dicts.append(dict_person)

will produce the result;

[{'name': 'Sammy', 'hobby': 'football', 'age': 6},
 {'name': 'Angela', 'hobby': 'chess', 'age': 12}]
  • Related