I am trying to write a Python dictionary to a file in json notation. That is how I have tried that:
def write_to_json(self, data):
with open('dict.json', 'w') as file:
json.dump(data, file)
However, the dump
method fails for my dictionary. It says:
TypeError: key 23 is not a string
This is (indeed) correct, it should be Integer
, but because the data is orginally read from a csv file and gets manipulated, I cannout guarantee so I need to convert them.
How can I convert each item in a dicitonary (key as well as the values) into a string?
Here is how an example of the dictionary looks like (its basically key -> list):
{1: [5,6,8,6], 2: [7,8,9]...}