我有一个这样的字典列表:
list_of_dict = [
{'text': '"Some text1"',
'topics': ['Availability', 'Waits'],
'categories': ['Scheduler']},
{'text': 'Alot to improve'},
{'text': 'More text '}
]
我将其写入csv文件,如下所示:
^{pr2}$这将写入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 |
如何做到这一点?谢谢!在
对于每个
row
来说,最简单的方法可能是从一个包含所有必需列值的默认字典开始设置为False
,然后当读入list_of_dict
中的每一行时,您可以发现它是否包含所需的键,并相应地更新您的row
:给您一个
^{pr2}$text.csv
文件:相关问题 更多 >
编程相关推荐