有人可以解释我如何从python中的字典中提取特定字段

时间:2016-11-15 16:58:24

标签: python parsing dictionary

我使用的是python 2.7和ebay sdk v2

我有一个dict存储,我把它写入csv文件(尝试),问题是我只想要某些字段,我不想写出每一列

这是我的词典

{'itemSearchURL': 'http://www.ebay.co.uk/sch/i.html?LH_ItemCondition=1&_nkw=OMP+OD%2F1989&_ddo=1&_ipg=1&_pgn=1', 'paginationOutput': {'totalPages': '187', 'entriesPerPage': '1', 'pageNumber': '1', 'totalEntries': '187'}, 'ack': 'Success', 'timestamp': '2016-11-15T15:52:01.356Z', 'searchResult': {'item': [{'itemId': '322324027874', 'subtitle': '100% GENUINE OMP STEERING WHEEL - NOT A CHEAP FAKE COPY', 'globalId': 'EBAY-GB', 'title': 'OD/1989/NN OMP TRECENTO UNO SPORTS STEERING WHEEL 300mm in BLACK POLYURETHANE', 'country': 'GB', 'primaryCategory': {'categoryId': '40195', 'categoryName': 'Steering Wheels & Boss Kits'}, 'autoPay': 'false', 'galleryURL': 'http://thumbs3.ebaystatic.com/m/miOhEO1pDb2cff4pPcZpwIQ/140.jpg', 'shippingInfo': {'shippingType': 'Free', 'shipToLocations': ['AU', 'Americas', 'Europe', 'Asia'], 'shippingServiceCost': {'_currencyId': 'GBP', 'value': '0.0'}}, 'location': 'United Kingdom', 'topRatedListing': 'false', 'viewItemURL': 'http://www.ebay.co.uk/itm/OD-1989-NN-OMP-TRECENTO-UNO-SPORTS-STEERING-WHEEL-300mm-BLACK-POLYURETHANE-/322324027874', 'sellingStatus': {'currentPrice': {'_currencyId': 'GBP', 'value': '78.28'}, 'timeLeft': 'P25DT0H21M7S', 'convertedCurrentPrice': {'_currencyId': 'GBP', 'value': '78.28'}, 'sellingState': 'Active'}, 'paymentMethod': 'PayPal', 'isMultiVariationListing': 'false', 'condition': {'conditionId': '1000', 'conditionDisplayName': 'New'}, 'listingInfo': {'listingType': 'FixedPrice', 'gift': 'false', 'bestOfferEnabled': 'false', 'startTime': '2016-11-10T16:13:08.000Z', 'buyItNowAvailable': 'false', 'endTime': '2016-12-10T16:13:08.000Z'}}], '_count': '1'}, 'version': '1.13.0'}

这是我的代码部分无法正常工作

def WriteDictToCSV(csv_file,csv_columns,dict_data):
    try:
        with open(csv_file, 'w') as csvfile:
            writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
            writer.writeheader()
            for data in dict_data:
                writer.writerow(data)
    except IOError as (errno, strerror):
            print("I/O error({0}): {1}".format(errno, strerror))    
    return            

csv_columns = ['itemId','title','subtitle','viewItemURL']


currentPath = os.getcwd()
csv_file = currentPath + "/csv/items.csv"

WriteDictToCSV(csv_file,csv_columns,response.dict())

这是我的错误

 Traceback (most recent call last):
      File "/home/richard/workspace/ebay title search by csv/learning2.py", line 56, in <module>
        WriteDictToCSV(csv_file,csv_columns,response.dict())
      File "/home/richard/workspace/ebay title search by csv/learning2.py", line 45, in WriteDictToCSV
        writer.writerow(data)
      File "/usr/lib64/python2.7/csv.py", line 148, in writerow
        return self.writer.writerow(self._dict_to_list(rowdict))
      File "/usr/lib64/python2.7/csv.py", line 144, in _dict_to_list
        ", ".join(wrong_fields))
    ValueError: dict contains fields not in fieldnames: i, t, e, m, S, e, a, r, c, h, U, R, L

我明白我错过了列名,但我真的不知道如何解析我想要的那些dict我想想将dict转换为xml并使用etree看到网上的一些指南怎么做但是真的想学习如何使用dict,因为它是

我也尝试过for循环,但这只是给了我错误,说没有价值

for item in response.dict()['searchResult']['item']:
    print "ItemID: %s" % item['itemId'].value
    print "Title: %s" % item['title'].value
    print "CategoryID: %s" % item['primaryCategory']['categoryId'].value

我正在努力让我的头围绕着容器

如果我通过dict执行for循环,我可以看到键和值。

mydata = response.dict()  
for key, value in mydata.items():
    print key, value

给我一​​个按键输出

我正在考虑枚举它们并提取单个键,但是读到字典没有被排序,所以它会产生不同的结果是真的。

1 个答案:

答案 0 :(得分:0)

您正在迭代传入的单字典

for data in dict_data:
    writer.writerow(data)

这将字典中的每个作为单独的行传递;每行应该是一个字典,csv模块使用行上的迭代来测试额外的键。这就是您从第一个键(itemSearchURL这里)看到每个字母的原因。

传入隐藏在['searchResult']['item']键下的词典列表

WriteDictToCSV(csv_file, csv_columns, response.dict()['searchResult']['item'])

现在dict_data是一系列dict个对象,data将是列表中的字典之一。

接下来,这些词典包含更多键而不是您对DictWriter实例所说的内容,因此您仍然会收到错误:

ValueError: dict contains fields not in fieldnames: 'globalId', 'country', 'primaryCategory', 'autoPay', 'galleryURL', 'shippingInfo', 'location', 'topRatedListing', 'sellingStatus', 'paymentMethod', 'isMultiVariationListing', 'condition', 'listingInfo'

告诉DictWriter忽略这些extrasaction='ignore'

writer = csv.DictWriter(out, fieldnames=csv_columns, extrasaction='ignore')

最后但同样重要的是,你不必自己做任何循环;只需使用writer.writerows()(复数,请注意末尾的s)一次写一行列表:

writer = csv.DictWriter(out, fieldnames=csv_columns, extrasaction='ignore')
writer.writeheader()
writer.writerows(dict_data)

演示:

>>> from cStringIO import StringIO
>>> import csv
>>> response_dict = {'itemSearchURL': 'http://www.ebay.co.uk/sch/i.html?LH_ItemCondition=1&_nkw=OMP+OD%2F1989&_ddo=1&_ipg=1&_pgn=1', 'paginationOutput': {'totalPages': '187', 'entriesPerPage': '1', 'pageNumber': '1', 'totalEntries': '187'}, 'ack': 'Success', 'timestamp': '2016-11-15T15:52:01.356Z', 'searchResult': {'item': [{'itemId': '322324027874', 'subtitle': '100% GENUINE OMP STEERING WHEEL - NOT A CHEAP FAKE COPY', 'globalId': 'EBAY-GB', 'title': 'OD/1989/NN OMP TRECENTO UNO SPORTS STEERING WHEEL 300mm in BLACK POLYURETHANE', 'country': 'GB', 'primaryCategory': {'categoryId': '40195', 'categoryName': 'Steering Wheels & Boss Kits'}, 'autoPay': 'false', 'galleryURL': 'http://thumbs3.ebaystatic.com/m/miOhEO1pDb2cff4pPcZpwIQ/140.jpg', 'shippingInfo': {'shippingType': 'Free', 'shipToLocations': ['AU', 'Americas', 'Europe', 'Asia'], 'shippingServiceCost': {'_currencyId': 'GBP', 'value': '0.0'}}, 'location': 'United Kingdom', 'topRatedListing': 'false', 'viewItemURL': 'http://www.ebay.co.uk/itm/OD-1989-NN-OMP-TRECENTO-UNO-SPORTS-STEERING-WHEEL-300mm-BLACK-POLYURETHANE-/322324027874', 'sellingStatus': {'currentPrice': {'_currencyId': 'GBP', 'value': '78.28'}, 'timeLeft': 'P25DT0H21M7S', 'convertedCurrentPrice': {'_currencyId': 'GBP', 'value': '78.28'}, 'sellingState': 'Active'}, 'paymentMethod': 'PayPal', 'isMultiVariationListing': 'false', 'condition': {'conditionId': '1000', 'conditionDisplayName': 'New'}, 'listingInfo': {'listingType': 'FixedPrice', 'gift': 'false', 'bestOfferEnabled': 'false', 'startTime': '2016-11-10T16:13:08.000Z', 'buyItNowAvailable': 'false', 'endTime': '2016-12-10T16:13:08.000Z'}}], '_count': '1'}, 'version': '1.13.0'}
>>> csv_columns = ['itemId','title','subtitle','viewItemURL']
>>> out = StringIO()
>>> writer = csv.DictWriter(out, fieldnames=csv_columns, extrasaction='ignore')
>>> writer.writeheader()
>>> writer.writerows(response_dict['searchResult']['item'])
>>> print out.getvalue()
itemId,title,subtitle,viewItemURL
322324027874,OD/1989/NN OMP TRECENTO UNO SPORTS STEERING WHEEL 300mm in BLACK POLYURETHANE,100% GENUINE OMP STEERING WHEEL - NOT A CHEAP FAKE COPY,http://www.ebay.co.uk/itm/OD-1989-NN-OMP-TRECENTO-UNO-SPORTS-STEERING-WHEEL-300mm-BLACK-POLYURETHANE-/322324027874