0

我试图做的事情可能有一个术语,但它让我无法理解。我正在使用 peewee 在类中设置一些值,并希望遍历键和值列表以生成存储值的命令。

并非所有“集合”都包含类中的每个值,因此我只想包含我的数据集中包含的值。这是我做到了多远:

for value in result['response']['docs']:
    for keys in value:
        print keys, value[keys]   # keys are "identifier, title, language'



#for value in result['response']['docs']:
#   collection =  Collection(
#       identifier = value['identifier'],
#       title = value['title'],
#       language = value['language'],
#       mediatype = value['mediatype'],
#       description = value['description'],
#       subject = value['subject'],
#       collection = value['collection'],
#       avg_rating = value['avg_rating'],
#       downloads = value['downloads'],
#       num_reviews = value['num_reviews'],
#       creator = value['creator'],
#       format = value['format'],
#       licenseurl = value['licenseurl'],
#       publisher = value['publisher'],
#       uploader = value['uploader'],
#       source = value['source'],
#       type = value['type'],
#       volume = value['volume']
#       )
#   collection.save()
4

2 回答 2

2
for value in result['response']['docs']:
    Collection(**value).save()

有关如何工作的说明,请参阅此问题。**kwargs

于 2013-06-10T09:02:55.257 回答
1

您是在谈论如何找出密钥是否在字典中?

>>> somedict = {'firstname': 'Samuel', 'lastname': 'Sample'}

>>> if somedict.get('firstname'):
>>>    print somedict['firstname']
Samuel

>>> print somedict.get('address', 'no address given'):
no address given

如果您想解决其他问题,请澄清您的问题。

于 2013-06-10T09:02:13.303 回答