I have this dictionary of lists that I want to write it in a CSV file:
file2_Dict = {'id': ' ', 'gender': ['M', 'M'], 'name': ['Smith', 'John'], 'city': ['Omaha','Lincoln']}
The problem is that not all the keys of the dictionary have a list of items as a value. In this case the key (i.e.'id'
) has a string as value (i.e. " "
). I tried to use the solution provided by Tim Pietzcker, but this only outputs the header of the csv file (keys of the dictionary), like this:
id, gender, name, city
The expected output should be, like this:
id, gender, name, city
,M,Smith,Omaha
,M,John,Lincoln
Any idea how to solve this problem?