I have a list of dictionaries like this:
list_of_dict = [
{'text': '"Some text1"',
'topics': ['Availability', 'Waits'],
'categories': ['Scheduler']},
{'text': 'Alot to improve'},
{'text': 'More text '}
]
I am writing it to a csv file as follows:
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)
This writes to the csv file as follows:
text | topics | categories
Some text1 | ['Availability', 'Waits'] | ['Scheduler']
Alot to improve |
More text |
However, I want it that for each category and for each topic there should be a separate column, then if some topic exists from the topics list or some category exists from the categories list, then write True in that cell for that particular topic/category of the text else write False.
OUTPUT:
text | Availability | Waits | Scheduler |
Some text1 | True | True | True |
Alot to improve | False | False | False |
More text | False | False | False |
How can this be done? Thanks!