Home > Mobile >  Trouble with DictWriter when writing dictionary with list of values
Trouble with DictWriter when writing dictionary with list of values

Time:07-14

So i have a dictionary that was created after I scraped data from websites using selenium.

The format preview of the dictionary is as such. I printed the dictionary and it is working fine.

{'Website A': [apple,orange,pear], 'Website B': [apple,watermelon], 'Website C': [orange,pear]}

Below is my attempted code for the writing to csv. I understand that writerow requires some form of manipulation but how should I configure such that my list is correctly reflected each website? I tried to zip the dictionary values but it doesn't seem to display the data correctly. website_names is a list of website names that are the same as the keys in the dictionary.

with open('test.csv', 'w',  newline = '') as csvfile:
cols = website_names
writer = csv.DictWriter(csvfile, fieldnames = cols, delimiter =';', extrasaction='ignore')
writer.writeheader()
writer.writerow(final_dict.keys())
writer.writerows(zip(*final_dict.values()))

This is how I'd like the data to be written on the csv file.

Website Data
Website A apple, pear, orange
Website B apple, watermelon
Website C orange, pear

But this is how it is displaying right now, everything smushed into two rows.

Website A ; Website B; Website C
[apple,pear,orange];[apple,watermelon];[orange,pear]

Appreciate any help!

CodePudding user response:

Is this what you need? (refer to csv doc):

>>> a={'Website A': ['apple', 'orange', 'pear'], 'Website B': ['apple', 'watermelon'], 'Website C': ['orange', 'pear']}
>>> cols = ['Website', 'Data']
>>> f=[]
>>> for i in a:
...     f.append({cols[0]: i, cols[1]: a[i]})
... 
>>> with open('test.csv', 'w', newline='') as csvfile:
...     writer = csv.DictWriter(csvfile, fieldnames=cols)
...     writer.writeheader()
...     writer.writerows(f)
  • Related