nmorrill714
nmorrill714

Reputation: 35

How can I write a nested dictionary to a CSV file?

I'm trying to write a nested dictionary to a CSV file and running into issues; either the file doesn't write anything, or it errors out.

The dictionary looks something like this:

finalDict = 'How would you rate the quality of the product?': [{'10942625544': 'High '
                                                                    'quality'},
                                                    {'10942625600': 'Neither '
                                                                    'high nor '
                                                                    'low '
                                                                    'quality'},
                                                    {'10942625675': 'Neither '
                                                                    'high nor '
                                                                    'low '
                                                                    'quality'},
                                                    {'10942625736': 'Very high '
                                                                    'quality'},
                                                    {'10942625788': 'Neither '
                                                                    'high nor '
                                                                    'low '
                                                                    'quality'},
                                                    {'10942625827': 'Neither '
                                                                    'high nor '
                                                                    'low '
                                                                    'quality'},
                                                    {'10942625878': 'Neither '
                                                                    'high nor '
                                                                    'low '
                                                                    'quality'},
                                                    {'10942625932': 'High '
                                                                    'quality'},
                                                    {'10942625977': 'High '
                                                                    'quality'},
                                                    {'10942626027': 'Neither '
                                                                    'high nor '
                                                                    'low '
                                                                    'quality'},
                                                    {'10942626071': 'High '
                                                                    'quality'},
                                                    {'10942626128': 'High '
                                                                    'quality'},
                                                    {'10942626180': 'Very high '
                                                                    'quality'},
                                                    {'10942626227': 'Very high '
                                                                    'quality'},
                                                    {'10942626278': 'High '
                                                                    'quality'},
                                                    {'10942626332': 'Low '
                                                                    'quality'},
                                                    {'10942626375': 'Very high '
                                                                    'quality'},
                                                    {'10942626430': 'Low '
                                                                    'quality'},
                                                    {'10942626492': 'Low '
                                                                    'quality'}],
 'How would you rate the value for money of the product?': [{'10942625544': 'Above '
                                                                            'average'},
                                                            {'10942625600': 'Below '
                                                                            'average'},
                                                            {'10942625675': 'Average'},
                                                            {'10942625736': 'Excellent'},
                                                            {'10942625788': 'Above '
                                                                            'average'},
                                                            {'10942625827': 'Below '
                                                                            'average'},
                                                            {'10942625878': 'Average'},
                                                            {'10942625932': 'Average'},
                                                            {'10942625977': 'Above '
                                                                            'average'},
                                                            {'10942626027': 'Above '
                                                                            'average'},
                                                            {'10942626071': 'Above '
                                                                            'average'},
                                                            {'10942626128': 'Average'},
                                                            {'10942626180': 'Excellent'},
                                                            {'10942626227': 'Average'},
                                                            {'10942626278': 'Average'},
                                                            {'10942626332': 'Below '
                                                                            'average'},
                                                            {'10942626375': 'Excellent'},
                                                            {'10942626430': 'Poor'},
                                                            {'10942626492': 'Below '
                                                                            'average'}],

I've tried working off of Write Nested Dictionary to CSV but am struggling to adapt it to my specific case.

My code currently looks like:

def writeToCsv(finalDict):
    csv_columns = ['Question', 'UserID', 'Answer']
    filename = "output.csv"
    with open(filename, "w") as filename:
        w = csv.DictWriter(filename, fieldnames=csv_columns)
        w.writeheader()
        for data in finalDict: #where I'm stuck

Any recommendations would be appreciated!

Upvotes: 1

Views: 1440

Answers (2)

Dun Peal
Dun Peal

Reputation: 17679

for question, data in finalDict.items():
   for resp in data:
       row = {'Question': question,
              'UserID': list(resp.keys())[0],
              'Answer': list(resp.values())[0]}
       w.writerow(row)

Upvotes: 2

r.ook
r.ook

Reputation: 13858

This is an option:

def writeToCsv(finalDict):
    csv_columns = ['Question', 'UserID', 'Answer']
    filename = "output.csv"
    with open(filename, "w") as fl:
        w = csv.DictWriter(fl, fieldnames=csv_columns, lineterminator='\n')
        w.writeheader()
        for question, data in finalDict.items()
            for item in data:
                for user, answer in item.items():
                    w.writerow(dict(zip(csv_columns, (question, user, answer))))

Upvotes: 2

Related Questions