For a writerow of a csv.DictWriter, I need to fill a (part of a) dictionary to be empty. I found no immediate solution for it. Is there one?
Like with
fieldnames = ['first','last']
Pseudocode
row = fieldnames.fill(None)
Result
print(row)
['first':None,'last':None]
So that
destination.writerow(row)
results in
,
This is really a natural for the built-in dict method fromkeys:
>>> dict.fromkeys('abcd',None)
{'a': None, 'c': None, 'b': None, 'd': None}
>>> dict.fromkeys(['first','last'],None)
{'last': None, 'first': None}
No need for a dict comprehension (2.7+) or a list comprehension at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With