在Python中将CSV转换为结构良好的JSON

时间:2015-03-12 00:58:13

标签: python json python-2.7 itertools

我有一个CSV文件,其结构如下:

Store, Region, District, MallName, Location

1234,90,910,MallA,GMT
4567,87,902,MallB,EST
2468,90,811,MallC,PST
1357,87,902,MallD,CST

我通过迭代眉头拍得到的结果是得到了这样的格式:

{
  "90": {
    "910": {
      "1234": {
        "name": "MallA",
        "location": "GMT"
      }
    },
    "811": {
      "2468": {
        "name": "MallB",
        "location": "PST"
      }
    }
  },
  "87": {
    "902": {
      "4567": {
        "name": "MallB",
        "location": "EST"
      },
      "1357": {
        "name": "MallD",
        "location": "CST"
      }
    }
  }
}

下面的代码被剥离以匹配我提供的示例数据集,但您可以了解发生了什么。再次,它是非常迭代和非pythonic,我也试图走向。 (如果有人觉得定义的程序值得发布我可以)。

#************
#   Main()
#************
dictHierarchy = {}

with open(getFilePath(), 'r') as f:
    content = [line.strip('\n') for line in f.readlines()]

for data in content:
    data = data.split(",")

    myRegion = data[1]
    myDistrict = data[2]
    myName = data[3]
    myLocation = data[4]
    myStore = data[0]

    if myRegion in dictHierarchy:
        #check for District
        if myDistrict in dictHierarchy[myRegion]:
            #checkforStore
            dictHierarchy[myRegion][myDistrict].update({myStore:addStoreDetails(data)})
        else:
            #add district
            dictHierarchy[myRegion].update({myDistrict:addStore(data)}) 
    else:
        #add region
        dictHierarchy.update({myRegion:addDistrict(data)})

with open('hierarchy.json', 'w') as outfile:
    json.dump(dictHierarchy, outfile)

强迫性的我看了上面的JSON输出,并认为有人盲目地打开文件,它看起来像一个大杂烩。我希望为纯文本可读性做的是将数据分组并将其转换为JSON格式,如下所示:

{"Regions":[
    {"Region":"90", "Districts":[
        {"District":"910", "Stores":[
            {"Store":"1234", "name":"MallA", "location":"GMT"}]},
        {"District":"811", "Stores":[
            {"Store":"2468", "name":"MallC", "location":"PST"}]}]},
    {"Region":"87", "Districts":[
        {"District":"902", "Stores":[
            {"Store":"4567", "name":"MallB", "location":"EST"},
            {"Store":"1357", "name":"MallD", "location":"CST"}]}]}]}

长话短说我今天浪费了相当多的时间试图弄清楚如何在Python中实际填充数据结构,并且基本上没有在哪里结束。是否有一种干净,pythonic的方式来实现这一目标?是否值得努力?

1 个答案:

答案 0 :(得分:1)

我已在您的输入中添加标题,如:

Store,Region,District,name,location
1234,90,910,MallA,GMT
4567,87,902,MallB,EST
2468,90,811,MallC,PST
1357,87,902,MallD,CST

然后使用python csv readergroup by这样:

import csv
from itertools import groupby, ifilter
from operator import itemgetter

data = []

with open('in.csv') as csvfile:
    reader = csv.DictReader(csvfile)

    regions = []

    regions_dict = sorted(list(reader), key=itemgetter('Region'))
    for region_id, region_group in groupby(regions_dict, itemgetter('Region')):

        districts = []
        regions.append({'Region': region_id, 'Districts': districts})

        districts_dict = sorted(region_group, key=itemgetter('District'))
        for district_id, district_group in groupby(districts_dict, itemgetter('District')):
            districts.append({'District': district_id, 'Stores': list(district_group)})

print regions