使用python将字典值作为单独的列写入csv中

时间:2017-09-07 20:18:54

标签: python csv dictionary

我有一个这样的词典列表:

list_of_dict = [
    {'text': '"Some text1"', 
     'topics': ['Availability', 'Waits'], 
     'categories': ['Scheduler']},
    {'text': 'Alot to improve'},
    {'text': 'More text '}
    ]

我将它写入csv文件,如下所示:

with open("text.csv", 'wb') as resultFile:
            wr = csv.writer(resultFile, dialect='excel')
            wr.writerow(['text', 'topics', 'categories'])

for d in list_of_dict:
    with open("text.csv", 'a') as f:
            w = csv.DictWriter(f, d.keys())
            w.writerow(d)

这将写入csv文件,如下所示:

text            |  topics                   | categories
Some text1      | ['Availability', 'Waits'] | ['Scheduler']
Alot to improve |
More text       |

但是,我希望每个类别和每个主题都应该有一个单独的列,如果topics列表中存在某个主题或categories列表中存在某个类别,则在该单元格中为该特定主题/文本类别写True,然后写False

输出:

text             | Availability | Waits | Scheduler |
Some text1       | True         | True  | True      |
Alot to improve  | False        | False | False     |
More text        | False        | False | False     |

如何做到这一点?谢谢!

1 个答案:

答案 0 :(得分:0)

对于每个row,最简单的方法是从包含设置为False的所有必需列值的默认字典开始,然后在读入list_of_dict中的每一行时,可以发现它是否包含所需的密钥,并相应地更新row

import csv

list_of_dict = [
    {'text': '"Some text1"', 'topics': ['Availability', 'Waits'], 'categories': ['Scheduler']},
    {'text': 'Alot to improve'},
    {'text': 'More text '}]

all_topics = ["Availability", "Waits"]
all_categories = ["Scheduler"]
fieldnames = ["text"] + all_topics + all_categories

with open("text.csv", 'wb') as f_output:
    csv_output = csv.DictWriter(f_output, fieldnames=fieldnames, dialect='excel')
    csv_output.writeheader()

    for d in list_of_dict:
        # Build a default row
        row = {v:False for v in all_topics + all_categories}
        row['text'] = d['text'].strip('"')

        if 'topics' in d:
            row.update({topic:True for topic in d['topics']})
        if 'categories' in d:
            row.update({category:True for category in d['categories']})

        csv_output.writerow(row)

给你一个text.csv档案:

text,Availability,Waits,Scheduler
Some text1,True,True,True
Alot to improve,False,False,False
More text ,False,False,False