I\'m struggling with writing a dictionary of lists to a .csv file.
This is how my dictionary looks like:
dict[key1]=[1,2,3]
dict[key2]=[4,5,6]
dict[k
This will work even when the list in key are of different length.
with myFile:
writer = csv.DictWriter(myFile, fieldnames=list(clusterWordMap.keys()))
writer.writeheader()
while True:
data={}
for key in clusterWordMap:
try:
data[key] = clusterWordMap[key][ind]
except:
pass
if not data:
break
writer.writerow(data)
You can use pandas for saving it into csv:
df = pd.DataFrame({key: pd.Series(value) for key, value in dictmap.items()})
df.to_csv(filename, encoding='utf-8', index=False)