[英]Python: Extract Attribute within JSON data structure
我目前正在使用eBay公共API解析eBay數據,我已經找到了解析JSON結構的方法,除了一些JSON元素。
這是我正在查看的JSON結構:
{u'itemId': [u'370640300983'], u'isMultiVariationListing': [u'false'], u'globalId': [u'EBAY-US'], u'title': [u'DELL Latitude D630 Core 2 Duo 2GHz 1GB 80GB CD-RW/DVD WiFi Notebook 14" Laptop'], u'country': [u'US'], u'shippingInfo': [{u'expeditedShipping': [u'true'], u'shippingType': [u'Calculated'], u'handlingTime': [u'1'], u'shipToLocations': [u'US'], u'oneDayShippingAvailable': [u'false']}], u'galleryURL': [u'http://thumbs4.ebaystatic.com/pict/3706403009834040_1.jpg'], u'autoPay': [u'false'], u'location': [u'Saint Paul,MN,USA'], u'postalCode': [u'55114'], u'returnsAccepted': [u'true'], u'viewItemURL': [u'http://www.ebay.com/itm/DELL-Latitude-D630-Core-2-Duo-2GHz-1GB-80GB-CD-RW-DVD-WiFi-Notebook-14-Laptop-/370640300983?pt=Laptops_Nov05'], u'sellingStatus': [{u'currentPrice': [{u'@currencyId': u'USD', u'__value__': u'99.99'}], u'timeLeft': [u'P0DT0H13M10S'], u'convertedCurrentPrice': [{u'@currencyId': u'USD', u'__value__': u'99.99'}], u'bidCount': [u'4'], u'sellingState': [u'Active']}], u'paymentMethod': [u'PayPal', u'VisaMC', u'Discover'], u'primaryCategory': [{u'categoryId': [u'177'], u'categoryName': [u'PC Laptops & Netbooks']}], u'condition': [{u'conditionId': [u'3000'], u'conditionDisplayName': [u'Used']}], u'listingInfo': [{u'listingType': [u'Auction'], u'gift': [u'false'], u'bestOfferEnabled': [u'false'], u'startTime': [u'2012-08-15T23:28:05.000Z'], u'buyItNowAvailable': [u'false'], u'endTime': [u'2012-08-20T23:28:05.000Z']}]}
我目前正在解析的數據
370640300983
DELL Latitude D630 Core 2 Duo 2GHz 1GB 80GB CD-RW/DVD WiFi Notebook 14" Laptop
{u'@currencyId': u'USD', u'__value__': u'99.99'}
第二要素:
{u'itemId': [u'170892723100'], u'isMultiVariationListing': [u'false'], u'globalId': [u'EBAY-US'], u'title': [u'Dell Latitude D620 Laptop Core 2 Duo 2GHz 1GB Ram No HDD INCOMPLETE'], u'country': [u'US'], u'shippingInfo': [{u'expeditedShipping': [u'false'], u'handlingTime': [u'1'], u'shippingServiceCost': [{u'@currencyId': u'USD', u'__value__': u'24.0'}], u'oneDayShippingAvailable': [u'false'], u'shipToLocations': [u'US'], u'shippingType': [u'Flat']}], u'galleryURL': [u'http://thumbs1.ebaystatic.com/pict/1708927231004040_1.jpg'], u'autoPay': [u'false'], u'location': [u'Hughesville,PA,USA'], u'postalCode': [u'17737'], u'returnsAccepted': [u'true'], u'viewItemURL': [u'http://www.ebay.com/itm/Dell-Latitude-D620-Laptop-Core-2-Duo-2GHz-1GB-Ram-No-HDD-INCOMPLETE-/170892723100?pt=Laptops_Nov05'], u'sellingStatus': [{u'currentPrice': [{u'@currencyId': u'USD', u'__value__': u'20.01'}], u'timeLeft': [u'P0DT1H10M35S'], u'convertedCurrentPrice': [{u'@currencyId': u'USD', u'__value__': u'20.01'}], u'bidCount': [u'2'], u'sellingState': [u'Active']}], u'paymentMethod': [u'PayPal'], u'primaryCategory': [{u'categoryId': [u'177'], u'categoryName': [u'PC Laptops & Netbooks']}], u'condition': [{u'conditionId': [u'3000'], u'conditionDisplayName': [u'Used']}], u'listingInfo': [{u'listingType': [u'Auction'], u'gift': [u'false'], u'bestOfferEnabled': [u'false'], u'startTime': [u'2012-08-18T00:25:30.000Z'], u'buyItNowAvailable': [u'false'], u'endTime': [u'2012-08-21T00:25:30.000Z']}]}
第二個元素的解析元素:
170892723100
Dell Latitude D620 Laptop Core 2 Duo 2GHz 1GB Ram No HDD INCOMPLETE
{u'@currencyId': u'USD', u'__value__': u'20.01'}
如果你看到在我的代碼都重復我無法獲得U” 值 :元素解析,並從數據結構的實際價格摘錄:
基本上代替{u'@currencyId': u'USD', u'__value__': u'20.01'}
我希望獲得20.01
作為解析后的值。 我應該使用正則表達式來解析它還是有更好的方法呢?
Here is my code:
data = json.load(urllib2.urlopen(url))
#print data
for item in data['findItemsByKeywordsResponse'][0]['searchResult'][0]['item']:
print item
for itemId in item['itemId']:
print itemId
for title in item['title']:
print title
for price in item['sellingStatus'][0]['currentPrice']:
print price
print '\n'
只要這樣做:
for price in item['sellingStatus'][0]['currentPrice']:
print float(price["__value__"])
當然,使用浮點數賺錢是一個可怕的想法,因此您應該使用decimal
模塊:
from decimal import Decimal
for price in item['sellingStatus'][0]['currentPrice']:
print Decimal(price["__value__"])
或將其解析為以美分為單位的整數價格:
for price in item['sellingStatus'][0]['currentPrice']:
dollars, cents = price["__value__"].split(".")
print int(dollars) * 100 + int(cents)
(以上評論):
嘗試更改:
for price in item['sellingStatus'][0]['currentPrice']:
print price
至
for price in item['sellingStatus'][0]['currentPrice']:
print price['__value__']
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.