Ich muss eine große JSON-Datei mit ijson analysieren (sofern etwas nicht besser ist), ich möchte alle Produktnamen in der Anfrage durchlaufen und sie ausdrucken. Ich habe versucht, dies über diese Support-Seite einzurichten. https://pypi.python.org/pypi/ijson/Python Ijson große Datei Schleife, um Namen zu bekommen
Dies ist die aktuelle Ausgabe, die ich
<addinfourl at 140643118020800 whose fp = <socket._fileobject object at 0x7fea07882850>>
<generator object items at 0x7fea077dc910>
<generator object <genexpr> at 0x7fea077dc960>
Mein Code
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
Hier ist ein Stück der json Daten
{"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":
Ich verstehe nicht genau, was ich tun muss. mit dem Beispielcode, wenn ich Produkte = Liste (Objekt) mache, druckt es nichts, aber wenn ich Druckanforderung drucke, druckt es die gesamte Anfrage. – turtle02
@turtle Wenn Sie einen Generator haben, enthält er keinen Wert, er ist nur bereit, etwas zu liefern. Wenn Sie 'list (generator)' aufrufen, fragen Sie den 'generator' nach aktuellen Werten. Mit den Werten können Sie sie ausdrucken. Wenn Sie einen Generator drucken, sehen Sie nur eine Art von Funktion, aber nicht die Werte. –
Danke, das hat es behoben, aber jetzt bekomme ich diesen Fehler Datei "/ home/python/Desktop/ für Ereignis, Wert in basic_events: Datei" /usr/local/lib/python2.7/dist-packages/ijson/ backends/python.py ", Zeile 185, in basic_parse für Wert in parse_value (lexer): Datei" /usr/local/lib/python2.7/dist-packages/ijson/backends/python.py ", Zeile 108 , in parse_value pos, symbol = next (lexer) Datei "/usr/local/lib/python2.7/dist-packages/ijson/backends/python.py", Zeile 25, in Lexer, wenn type (f.read (0)) == bytetype: AttributeError: 'Response' Objekt hat kein Attribut 'read' – turtle02