使用groupby以及比较&访问dicts?

时间:2017-01-11 15:21:16

标签: python csv dictionary

尝试更好地理解从dicts访问数据,比较一个dict中的项目以查看它是否存在于另一个dict中,然后根据是否找到它来分配值。还尝试使用groupby来组织事物。

假设我有以下“inventory.csv”:

SECOND FORM STARTS SUBMIT -> FIRST FORM STARTS SUBMIT -> FIRST FORM FINISHES SUBMIT -> SECOND FORM FINISHES SUBMIT

我有以下“saleprice.csv”:

sku,item,category,price
111,apple,fruit,1.00
222,orange,fruit,1.25
333,lettuce,veggie,1.50
444,carrot,veggie,1.75

我的代码尝试如下:

sku,item,price
222,orange,1.00
444,carrot,1.50

我的第一个问题似乎是我在使用“groupby”时失败了。因为我不断得到与“必须是整数,而不是str”相关的错误。

我希望我的结果会像:

from __future__ import print_function
from itertools import groupby
import csv

readInv = csv.DictReader(open('inventory.csv'))
inventory = {}
for row in readInv:
    key = row.pop('sku')
    if key in inventory:
        pass
    inventory[key] = row
print (inventory)
categories = inventory.items()

readSale = csv.DictReader(open('saleprice.csv'))
saleprice = {}
for row in readSale:
    key = row.pop('sku')
    if key in saleprice:
        pass
    saleprice[key] = row

for key, group in groupby(categories, lambda x: x['category']):
    print ("New Price by Category: " + key + "\n")
    for category in group:
        sku = category['sku']
        item = category['item']
        if sku in saleprice:
            price = saleprice['price']
        else:
            price = category['price']

    print ('Item:     {0}, SKU:      {1}, Price:      {2}').format(item, sku, price)

如果我这样做,它会看到sku 222存在于saleprice dict中并且将使用该价格(1.00)来设置价格变量。同样适用于sku 444。

在salesprice dict中不存在sku 111和333所以它应该使用库存中的价格。

在这种情况下,我绝对可以使用他人的知识来帮助理解如何正确使用“groupby”。然后进行适当的检查以确定应将值分配给“价格”。

1 个答案:

答案 0 :(得分:0)

使用itertools.groupby

可以实现
Input    Intended output
b'\x00'  0 or '0'
b'\x01'  1 or '1'

输出

import csv
import itertools
import operator


InvReader = csv.DictReader(open('inv.txt'))
SaleReader  = csv.DictReader(open('saleprice.txt'))

dictSaleReader = list(SaleReader)
updatedinv = []

for Invline in InvReader: 
    print Invline
    IsMatch=False
    for SalseLine in dictSaleReader:        
        if Invline['sku'] == SalseLine['sku']: # if a matching Item found in saleprice update price
            Invline['price']=SalseLine['price']
            updatedinv.append((Invline))
            IsMatch=True
    if IsMatch==False:
        updatedinv.append((Invline))

for category, group in itertools.groupby(updatedinv, key=operator.itemgetter('category')):
     print ("\nNew Price by Category: " + category + "\n")
     for category in group:
         print ('Item:     {0}, SKU:      {1}, Price:      {2}').format(category['item'], category['sku'], category['price'])