I have a nested dictionary of key, value pairs structured like this:
(0, {'hoovervilles': 13, 'depression.': 10, 'everyday:': 6}), (1, {'mother': 10, 'child': 9, 'daughter': 3, 'prevail': 1}), (2, {'music': 6, 'style,': 2, 'listening': 2})
For each item in this dictionary, I want to write out a tsv with the keys and values.
This writes keys, values for a single item from the dictionary:
csv_file = '.../frequencies_dir/2.tsv'
try:
with open(csv_file, 'w') as f:
for key, value in entry.items():
f.write("%s\t%s\n"%(key,entry[key]))
except IOError:
print("I/O error")
How can I write each item from the dictionary to a separate file (tsv) in a directory and name the tsv file based on its item number in the dictionary (e.g. 2.tsv, 3.tsv, etc.)
Your input data is not valid in python. But, if it is something like below :
You can write :