如何将嵌套字典写入CSV文件?

时间:2019-12-05 16:20:21

标签: python csv dictionary

我正在尝试将嵌套字典写入CSV文件并遇到问题;该文件没有写任何东西,或者出错了。

字典看起来像这样:

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'}],

我已经尝试过Write Nested Dictionary to CSV,但正在努力使其适应我的具体情况。

我的代码当前如下所示:

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

任何建议将不胜感激!

2 个答案:

答案 0 :(得分:1)

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)

答案 1 :(得分:1)

这是一个选择:

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))))