Trouble parsing API JSON output with python using 'requests' -
So I'm trying to get the date and average price of DateCoin from Wokofettochi (a cryptocurrency, for those unfamiliar people ) I get an error using COM API requests and whenever I try to run the following code
import request content = requests.get ("https: // api. Vaultofsatoshi.com/public/ticker? Order_currency = DOGE and payment_currency = USD ") Print content. Here's the error: Traceback (most recent): [Jason () [{"Data": {"Date", "Avg_Price"}}]
Call final): File "Filename", & lt; Module & gt; Print content Jason () [{"Data": {"Date", "Average_Price"}} Type Type: Undeclared Type: 'dict'
I'm new to API and to clarify Request modules I am not very familiar with syntax, any help is appreciated.
Edit: A typo is fixed with the URL and for reference, this is the word that will return it.
{"Status": "Success", "Data": { "Date": 1392701294, "Initial_price": {"Exact": 5, "Value": "0.00150000", "Value_ int": 150}, "Value": "", "Value": "," Value "Value": "value": "value": "value": "value": "value": "0.00143000", "value_ int": 153}, "units_trared": {"precise": 8, "value": "15 941676.33311552", "price_directory" Value ":" value ":" value ":" value ":" value_ int ": 154}," min_price ": {" exact ": 5," value ":" value ":" 141 "," average ":" "" Value ":" Original "": "0.00148", "value_ int": 148}, "quantity_1 days": {"exact": 8, "value": "15 9 41676.33311552", "Value_int": 1594167633311552}, "Volume_7": {"Precise": 8, "value": "115024501.70386628", "value_ int": 115024501703866 28}}}request response method other Gives dictionaries to dictionaries
First you are looking at JSON objects back. We will use the results to print beautifully so that we can understand their structure.
Import request Import pprint content = requests.get ( "https://api.vaultofsatoshi.com/publi/ ticker? Order_currency = DOGE and Bhugtan_ currency = USD") j = contents.json () Pprint.pprint (j)
it shows:
{u'data}: {U'average_price ': {u'precision': 5, u'value ': u'0.00148', 'value_int': 148}, u'closing_price ': {U'precision': 5, u'value ': U'0.00152990 ', u'value_int': 152}, Utat ': 1392701954, U'max_price': {U'prisijan ': 5, you'veview: U'0.00154900, you' value ': 154}, you 'In_Price': {You 'Prestige': 5, U 'value: U'0.00141000', U 'value': 141}, U 'Opening_pierce Hi ': (u'precision': 5, u'value ': u'0.00150000', u'value_int ': 150}, u'units_traded': {u'precision ': 8, u'value': u'15946541.33311552 "U'valu_int 'L594654l333ll552L} U'volum_lday': {U'prisisn ': 8, U'valu' U'l594654lk333ll552, U'valu_int ': 1594654133311552}} U'volum_7day': {A ' Precision ': 8, u'value': u'114969451.70386628 ', u'value_int': 11496945170386628L}}, u'status ': u'success'}
So now, Need to walk through that data so that's what you want. Based on your snippet, I guess you want your date and average_pris field. First of all, we want the data area (though you should also check
status
).
data = j [ 'data']
Now, you want to date:
from datetime import datetime date dt = datetime.fromtimestamp (data [ ''])
and average price, however, there are several sub-fields Let's just grab the floating point version, and converted to float:
avg_price = float (data [ 'average_price'] [ 'value'])
now print it ...
print 'date: {0} average value: {1}'. Format (DT, Aji_pis)
And there you have it:
Date: 2014-02- 18 00:39:14 average value: 0.00148
Comments
Post a Comment