I've been trying to make a csv reader that will read a large data set, group the same events together, and count up how many times those events happened. I'm using nested dictionaries to do it.
For some reason the formatting is wrong and it keeps adding spaces between every single character in the string I'm writing.
Does anyone know how to remove it?
I've tried a few other things I've seen on here such as changing write to 'wb' and adding a new line argument, but I couldn't get any of them to work.
with open(r'Redacted', 'w') as reportCSV:
writer = csv.writer(reportCSV, delimiter=' ')
for dates, info in dict.items():
for key in info:
writer.writerow(dates + ':' + key + ':' + str(info[key]))
writerowiterates itsrowparameter, converts it to a string, applies any needed escapes and usesdelimiteras the separator. In your case, it appears as thoughdates + ':' + key + ':' + str(info[key])is a string.writerowiterates it character by character and inserts the' 'delimiter.It appears that you want 3 semi-colon separated columns. That third column could iterated directly via a second
.items()call. Put them in a list for the writer.