简体   繁体   English

Python CSV到JSON列的顺序

[英]Python csv to json column order

Basic Python csv to json script gets the column order mixed up in the final JSON. 基本的Python csv to json脚本将列顺序混合在最终JSON中。 Any idea why? 知道为什么吗?

test.csv test.csv

animal,age,count,legs
dogs,3,5,4
cats,6,4,4
birds,2,1,2

script 脚本

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 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"},
]

It is because dictionary does not have any sense of order, so it is expected that the dictionaries are in arbitrary order. 这是因为字典没有任何顺序感,因此期望字典以任意顺序排列。

If you must preserve the order (and ideally you should not have to), you would need to read each row using a simple csv reader, and then create collection.OrderedDict objects, that do store the order in which keys are added. 如果必须保留顺序(理想情况下不必这样做),则需要使用简单的csv阅读器读取每一行,然后创建collection.OrderedDict对象,该对象确实存储了添加键的顺序。 Example - 范例-

from collections import OrderedDict
import csv
with open('test.csv','r') as f:
    reader = csv.reader(f)
    headerlist = next(reader)
    csvlist = []
    for row in reader:
            d = OrderedDict()
            for i, x in enumerate(row):
                    d[headerlist[i]] = x
            csvlist.append(d)

import json
with open('test.json','w') as f:
    json.dump(csvlist,f)

Please note this would still be useless if the parser who would be using this JSON does not respect the order in the same way. 请注意,如果将使用此JSON的解析器不以相同的方式遵守顺序,则这仍然没有用。


Example/Demo - 示例/演示-

With my test.csv as - 与我的test.csv作为-

animal,age,count,legs
dogs,3,5,4
cats,6,4,4
birds,2,1,2

test.json looked like - test.json看起来像-

[{"animal": "dogs", "age": "3", "count": "5", "legs": "4"}, 
{"animal": "cats", "age": "6", "count": "4", "legs": "4"}, 
{"animal": "birds", "age": "2", "count": "1", "legs": "2"}]

In Anand's answer you are only temporarily solving the problem. 用阿南德的答案,您只是暂时解决问题。 The JSON file may be output in the correct order, but then you could have this data read in another location (such as the browser or another Python process), and when it reads in this file, the order will not be preserved! 可以按正确的顺序输出JSON文件,但是您可以在其他位置(例如浏览器或其他Python进程)读取此数据,并且在读取该文件时,将不会保留该顺序!

You will want to use a list of lists if you want to guarantee order between your data source and its destination. 如果要保证数据源与其目的地之间的顺序,您将希望使用列表列表

You can do the following to preserve order: 您可以执行以下操作来保留顺序:

import csv 
import json

lst = []
csvfile = open('test.csv', 'r')
jsonfile = open('test.json', 'w')
first_line = next(csvfile).split(',')

csvfile.seek(0) # we peeked at the first_line, lets reset back to beginning

reader = csv.DictReader(csvfile)

for row in reader:
    group = []
    for h in first_line:
        h = h.strip()
        group.append([h, row[h]])
    lst.append(group)

jsonfile.write(json.dumps(lst))

Output: 输出:

[[["animal", "dogs"], ["age", "3"], ["count", "5"], ["legs", "4"]], [["animal", "cats"], ["age", "6"], ["count", "4"], ["legs", "4"]], [["animal", "birds"], ["age", "2"], ["count", "1"], ["legs", "2"]]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM