In a csv file, I want to add all unique items to one row and then move to the next row. the names of the (to be filled up) rows are in the header (list), and the items are saved in the dict.
My code looks like this:
if key in dict:
for item in dict[key]:
with open(file, 'a') as obj:
csv_writer = csv.DictWriter(obj, skipinitialspace=True, fieldnames=header)
csv_writer.writerow({key:item})
Right now one row is filled with an emty space under each item and when the next key is called it doesn't start back up again. It looks like this:
animals | colours | surname |
---|---|---|
rat | ||
hourse | ||
red | ||
blue | ||
green | ||
smith | ||
I want it to look like that:
animals | colours | surname |
---|---|---|
rat | red | smith |
hourse | blue | |
green |
Why is is making the extra empty row and how can I make it start filling up the rows directly under the header?