Answer the question
In order to leave comments, you need to log in
How to add multiple dictionaries to CSV (Python)?
Converting a list to a dictionary using the Counter class, do I need to output everything to CSV? Here is the code that is there:
list1 = ['from=<[email protected]>', 'from=<[email protected]>', '[email protected]', '[email protected]', ]
list2 = ['to=<[email protected]>', 'to=<[email protected]>', 'to=<[email protected]>', ]
countList1 = Counter(list1)
countList2 = Counter(list2)
with open('outputTest.csv', 'w') as csvFile:
writer = csv.writer(csvFile)
header = ['email_from', 'quantity' 'email_to', 'quantity']
writer.writerow(header)
for item in countlister1:
writer.writerow((item, counLister1[item])) # Только для одного словаря
Answer the question
In order to leave comments, you need to log in
list1 = ['from=<[email protected]>', 'from=<[email protected]>', '[email protected]', '[email protected]', ]
list2 = ['to=<[email protected]>', 'to=<[email protected]>', 'to=<[email protected]>', ]
# сформировать сколько нужно словарей по списку
counters = [Counter(x) for x in [list1, list2]]
with open('outputTest.csv', 'w') as csvFile:
writer = csv.writer(csvFile)
header = ['email_from', 'quantity' 'email_to', 'quantity']
writer.writerow(header)
for counter in counters:
for item, cnt in counter.items():
writer.writerow((item, cnt))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question