将CSV排序以在Python中写入JSON文件

时间:2017-09-04 10:35:26

标签: python json csv sorting

我有一个CSV文件,使用Python脚本转换为JSON文件。它工作但我希望JSON具有相同的CSV文件排序。这是我试过没有成功的原因:

#!/usr/bin/python

import csv
import json

CSV_PATH = './myFile.csv'
JSON_PATH = './myFile.json'

csv_file = csv.DictReader(open(CSV_PATH, 'r'))

json_list = []
for row in csv_file:
   json_list.append(row)

sortedlist = sorted(json_list, key=lambda row:(row['id']), reverse=False)

file(JSON_PATH, 'w').write(json.dumps(
    sortedlist, sort_keys=False, indent=2, separators=(',', ': '), 
    encoding="utf-8",ensure_ascii=False))

我的CSV看起来像这样:

id,name,lastname
1,John,Red
2,Steve,Brown

但即使使用lambda函数,创建的JSON也是如此:

[
  {
    "name": "John",
    "id": "1",
    "lastname": "Red"
  },
  {
    "name": "Steve",
    "id": "2",
    "lastname": "Brown"
  }
]

我想得到的是“id”,“name”,然后是“lastname”。

3 个答案:

答案 0 :(得分:1)

字典不保留原始订单。 您可以尝试使用OrderedDict模块中的collection

from collections import OrderedDict
my_dictionary=OrderedDict()
my_dictionary['foo']=3
my_dictionary['bar']=1
my_dictionary
OrderedDict([('foo', 3), ('bar', 1)])

An OrderedDict is a dict that remembers the order that keys were first inserted.

它应该做的工作:

import json
from collections import OrderedDict
json.dumps(OrderedDict([("b", 1), ("a", 2), ("c", 2)]))

Out:'{“b”:1,“a”:2,“c”:2}'

json.dumps({'b': 1, 'a': 2, 'c': 2})

Out:'{“a”:2,“c”:2,“b”:1}'

如果您想对dict重新排序,Dictreaderfieldnames作为位置参数:

FIELDNAMES = ['id', 'name', 'last_name']
csv_file = csv.DictReader(open(CSV_PATH, 'r'), fieldnames=FIELDNAMES)
json_list = []
for row in csv_file:
   # Reorder according to your needs
   data = OrderedDict([
       ('name', row['name']), ('last_name', row['last_name']), ('id', row['id'])])
   json_list.append(data)
   ...

答案 1 :(得分:0)

sorted正在对列表中的条目进行排序。所以,如果您的原始列表是:

[
  {
    "name": "Steve",
    "id": "2",
    "surname": "Brown"
  },
  {
    "name": "John",
    "id": "1",
    "surname": "Red"
  }
]

它会按正确的顺序排列。实际上,您的列表已经与CSV文件的顺序相同。您的问题是列表中的每个条目都是字典,字典不会保留其元素的顺序。

答案 2 :(得分:0)

使用collections.OrderedDict维护密钥的顺序:

import csv
import json
from collections import OrderedDict

CSV_PATH = 'myFile.csv'
JSON_PATH = 'myFile.json'

with open(CSV_PATH) as csv_file, open(JSON_PATH, 'w') as json_file:
    reader = csv.DictReader(csv_file)
    data = [OrderedDict((field, row[field]) for field in reader.fieldnames) for row in reader]
    json.dump(data, json_file, indent=2, separators=(',', ': '), encoding="utf-8",ensure_ascii=False)

输入CSV文件的输出:

[
  {
    "id": "1",
    "name": "John",
    "surname": "Red"
  },
  {
    "id": "2",
    "name": "Steve",
    "surname": "Brown"
  }
]

此代码迭代CSV文件的行并创建OrderedDict的列表。密钥的顺序由fieldnames对象的DictReader列表维护。

然后使用json.dump()优先于json.dumps()将列表直接输出到文件,因为它稍微容易一些。