I have been trying to convert a csv file into json with python 3. I want to convert a csv file which would look more or less like this:
Key ,Field_1 ,Field_2 ...
0 ,a ,e ...
1 ,b ,f ...
2 ,c ,g ...
3 ,d ,h ...
Into a json file that is structured Key : [Field_1, Field_2, ...]
So that the csv above would end up looking like
{
"0" : ["a", "e", ...]
"1" : ["b", "f", ...]
"2" : ["c", "g", ...]
"3" : ["d", "h", ...]
}
I have been trying to use the code below but have been unable to fill in the missing parts, like assigning each value to their corresponding part on the json file.
csv = pd.read_csv("file.csv")
n = 0
length = # number of keys
for i in csv:
if n == 0:
for y in range(lentgh):
# Assign as the header
else:
for y in range(length):
# Assign as the properties
n += 1
Also Im trying to make it so that all new data is attached at the end of the json file automatically.