使用csv.DictReader创建嵌套的dict

时间:2015-07-24 00:27:22

标签: python python-2.7 csv

我正在使用python csv module,我有一个包含3列的CSV,项目,部分,类别。

我想创建一个结合了所有类别的dict,然后使用Item:Part对它们的值进行排序。

例如:

512 SSD SATA,42-000153,Hardware
5M DisplayPort 1.2 Cable,42-000135,Cable
90W AC Adapter,42-000146,Adapter
4 port USB hub,42-000126,Adapter

我得到的结果是:

mydict = {
    Hardware:{512 SSD SATA:42-000153},
    Cable:{5M DisplayPort 1.2 Cable,42-000135},
    Adapter:{90W AC Adapter:42-000146},
    Adapter:{4 port USB hub:42-000126}
    }

这几乎让我在那里:

def build_dict(source_file):
    projects = defaultdict(dict)
    headers = ['Product', 'Part Number', 'Category']
    with open(source_file, 'rb') as fp:
        reader = csv.DictReader(fp, fieldnames=headers, dialect='excel',
                                skipinitialspace=True)
        for rowdict in reader:
            if None in rowdict:
                del rowdict[None]
            category = rowdict.pop("Category")
            projects[category] = rowdict
        return dict(projects)

source_file = 'test.csv'

我正在寻找的结果:

mydict = {
    Hardware:{512 SSD SATA:42-000153},
    Cable:{5M DisplayPort 1.2 Cable,42-000135},
    Adapter:{90W AC Adapter:42-000146,4 port USB hub:42-000126}
    }

请帮忙!

3 个答案:

答案 0 :(得分:3)

我会利用Python的内置函数来实现它:

import csv
from collections import defaultdict

mydict = defaultdict(dict)
with open('inventory.csv', 'rb') as inf:
    for row in csv.DictReader(inf, fieldnames=['Product', 'Part Number', 
                                               'Category']):
        mydict[row['Category']][row['Product']] = row['Part Number']

import json  # for pretty-printing result
print(json.dumps(mydict, indent=4))

输出:

{
    "Hardware": {
        "512 SSD SATA": "42-000153"
    },
    "Adapter": {
        "4 port USB hub": "42-000126",
        "90W AC Adapter": "42-000146"
    },
    "Cable": {
        "5M DisplayPort 1.2 Cable": "42-000135"
    }
}

FWIW,您也可以这样做,这需要更多的代码行,但会使内循环中的内容更具可读性。结果将是相同的。请注意,它使用csv.reader而不是csv.DictReader

import csv
from collections import defaultdict
from collections import namedtuple

Record = namedtuple('Record', ['product', 'part_number', 'category'])

mydict = defaultdict(dict)
with open('inventory.csv', 'rb') as inf:
    for rec in map(Record._make, csv.reader(inf)):
        mydict[rec.category][rec.product] = rec.part_number  # more readable

答案 1 :(得分:0)

这可能有效。

import csv
import sys

f = open(sys.argv[1], 'rt')

ret = {}
try:
    reader = csv.reader(f)
    for row in reader:
        ret[row[-1]]={" ".join(row[0:2]):row[-2]}
finally:
    f.close()
print str(ret)

答案 2 :(得分:0)

只需更改defaultdict即可为每个项目构建一个列表,您的代码将再次运行。

def build_dict(source_file):
    projects = defaultdict(list)
    headers = ['Product', 'Part Number', 'Category']
    with open(source_file, 'r') as fp:
        reader = csv.DictReader(fp, fieldnames=headers, dialect='excel',
                                skipinitialspace=True)
        for rowdict in reader:
            if None in rowdict:
                del rowdict[None]
                continue
            category = rowdict.pop("Category")
            projects[category].append(rowdict)
        return dict(projects)

source_file = 'test.csv'

输出:

{'Cable': [{'Part Number': '42-000135', 'Product': '5M DisplayPort 1.2 Cable'}], 'Adapter': [{'Part Number': '42-000146', 'Product': '90W AC Adapter'}, {'Part Number': '42-000126', 'Product': '4 port USB hub'}], 'Hardware': [{'Part Number': '42-000153', 'Product': '512 SSD SATA'}]}

使用json pretty print(感谢martineau)

{
    "Cable": [
        {
            "Part Number": "42-000135",
            "Product": "5M DisplayPort 1.2 Cable"
        }
    ],
    "Adapter": [
        {
            "Part Number": "42-000146",
            "Product": "90W AC Adapter"
        },
        {
            "Part Number": "42-000126",
            "Product": "4 port USB hub"
        }
    ],
    "Hardware": [
        {
            "Part Number": "42-000153",
            "Product": "512 SSD SATA"
        }
    ]
}