Базовый сценарий python csv для json получает столбец, смешанный в финальном JSON. Любая идея почему?Python csv to json column order
test.csv
animal,age,count,legs
dogs,3,5,4
cats,6,4,4
birds,2,1,2
сценарий
import csv
import json
csvfile = open('test.csv', 'r')
jsonfile = open('test.json', 'w')
reader = csv.DictReader(csvfile)
jsonfile.write('[')
for row in reader:
json.dump(row, jsonfile)
jsonfile.write(',\n')
jsonfile.write(']')
test.json
[{"count": "5", "age": "3", "legs": "4", "animal": "dogs"},
{"count": "4", "age": "6", "legs": "4", "animal": "cats"},
{"count": "1", "age": "2", "legs": "2", "animal": "birds"},
]
Вы можете использовать это, чтобы получить упорядоченный словарь http://pymotw.com/2/collections/ordereddict. html – Khalsa
json не заказывается. и не является питоновым. – njzk2
@ njzk2 делает это как 'Object.keys (json [0])' is – geotheory