我需要使用ijson解析一个大的json文件(除非有更好的办法),我想遍历请求中的所有产品名称并打印出来。我试图用这个支持页面来设置它。 https://pypi.python.org/pypi/ijson/python ijson大文件循环来获取名称
这是电流输出,我得到
<addinfourl at 140643118020800 whose fp = <socket._fileobject object at 0x7fea07882850>>
<generator object items at 0x7fea077dc910>
<generator object <genexpr> at 0x7fea077dc960>
我的代码
import json
import requests
import lxml
import ijson
import urllib
from urllib import urlopen
request = urlopen('www.jsonurl.com')
objects = ijson.items(request, 'items.name')
products = (o for o in objects if o ['type' == 'name'])
for product in products:
print product
print request
print objects
print products
这里是一块JSON数据
{"query":"*","sort":"relevance","responseGroup":"base","totalResults":5158058,"start":1,"numItems":10,"items":[{"itemId":7933617,"parentItemId":7933617,"name":"Nordic Ware Heavyweight Scone/Cornbread Pan","msrp":26.97,"salePrice":20.42,"upc":"011172016409","categoryPath":"Home/Kitchen & Dining/Cookware, Bakeware & Tools/Specialty Cookware","shortDescription":"<p>This Nordic Ware Scone Pan is made of a heavyweight cast aluminum. It can be used as a heavyweight scone or cornbread pan, and it is designed to cook your meal evenly and thoroughly. It features a non-stick interior coating for easy release and clean up.</p>","longDescription":"<b>Nordic Ware Heavyweight Scone/Cornbread Pan:</b><ul><li>Heavyweight cast aluminum</li><li>Ideal for scones and cornbread</li><li>Eight wedges</li><li>Cooks evenly and thoroughly</li><li>Non-stick interior coating for easy release and clean-up</li></ul>","thumbnailImage":"http://i5.walmartimages.com/dfw/dce07b8c-c739/k2-_6fb32a28-c090-4377-81d5-e83273124841.v1.jpg","mediumImage":"http://i5.walmartimages.com/dfw/dce07b8c-ddb3/k2-_6f7df9fa-cb2d-4faf-afbc-8fa4185add59.v1.jpg","largeImage":"http://i5.walmartimages.com/dfw/dce07b8c-5bd3/k2-_6635f62a-5e0b-4c4e-a93d-ee85643f7397.v1.jpg","productTrackingUrl":"http://linksynergy.walmart.com/fs-bin/click?id=|LSNID|&offerid=223073.7200&type=14&catid=8&subid=0&hid=7200&tmpid=1082&RD_PARM1=http%253A%252F%252Fwww.walmart.com%252Fip%252FNordicWare-Heavyweight-Scone-Cornbread-Pan%252F7933617%253Faffp1%253DpjiPu5Y7cvNmz4xZOAs5j7QlW2mZPVmc1DR3BvmrkB4%2526affilsrc%253Dapi","standardShipRate":4.97,"marketplace":false,"modelNumber":"1640","productUrl":"http://c.affil.walmart.com/t/api02?l=http%3A%2F%2Fwww.walmart.com%2Fip%2FNordicWare-Heavyweight-Scone-Cornbread-Pan%2F7933617%3Faffp1%3DpjiPu5Y7cvNmz4xZOAs5j7QlW2mZPVmc1DR3BvmrkB4%26affilsrc%3Dapi%26veh%3Daff%26wmlspartner%3Dreadonlyapi","customerRating":"4.7","numReviews":20,"customerRatingImage":"http://i2.walmartimages.com/i/CustRating/4_7.gif","categoryNode":"4044_623679_133020","bundle":false,"stock":"Available","addToCartUrl":"http://c.affil.walmart.com/t/api02?l=http%3A%2F%2Faffil.walmart.com%2Fcart%2FaddToCart%3Fitems%3D7933617%7C1%26affp1%3DpjiPu5Y7cvNmz4xZOAs5j7QlW2mZPVmc1DR3BvmrkB4%26affilsrc%3Dapi%26veh%3Daff%26wmlspartner%3Dreadonlyapi","affiliateAddToCartUrl":"http://linksynergy.walmart.com/fs-bin/click?id=|LSNID|&offerid=223073.7200&type=14&catid=8&subid=0&hid=7200&tmpid=1082&RD_PARM1=http%253A%252F%252Faffil.walmart.com%252Fcart%252FaddToCart%253Fitems%253D7933617%257C1%2526affp1%253DpjiPu5Y7cvNmz4xZOAs5j7QlW2mZPVmc1DR3BvmrkB4%2526affilsrc%253Dapi","giftOptions":
我不完全明白我需要做什么。与示例代码如果我做产品=列表(对象)它不会打印任何东西,但如果我打印请求它打印整个请求。 – turtle02
@turtle如果你有一个生成器,它不包含任何值,它只是准备提供一些东西。通过调用'list(generator)',你可以让'generator'检索实际值。有了这些价值观,你可以打印它们。打印一个发电机,你只会看到它是某种功能,但不是数值。 –
谢谢你修正了它,但现在我得到这个错误文件“/ home/python/Desktop/ for event,value in basic_events: File”/usr/local/lib/python2.7/dist-packages/ijson/ backends/python.py“,第185行,在basic_parse 中parse_value(lexer)中的值: 文件”/usr/local/lib/python2.7/dist-packages/ijson/backends/python.py“,第108行,在parse_value pos,symbol = next(词法分析器) 文件“/usr/local/lib/python2.7/dist-packages/ijson/backends/python.py”,第25行,在Lexer中如果type(f.read (0))== bytetype:AttributeError:'Response'对象没有属性'read' – turtle02