Reputation: 163
I'm currently stuck on how to change the output of a JSON file from having scientific notation to having a float for some of the keys in the JSON dict.
for example I need this:
{'message': '', 'result': [{'Ask': 8.982e-05, 'BaseVolume': 235.09663206, 'Bid': 8.9e-05, 'Created': '2017-06-06T01:22:35.727', 'High': 9.413e-05, 'Last': 8.878e-05, 'Low': 8.01e-05, 'MarketName': 'BTC-1ST', 'OpenBuyOrders': 408, 'OpenSellOrders': 6009, 'PrevDay': 8.375e-05, 'TimeStamp': '2017-09-27T02:17:44.677', 'Volume': 2678614.34426254},
to look like this:
{"success":true,"message":"","result":[{"MarketName":"BTC-1ST","High":0.00009413,"Low":0.00008010,"Volume":2678614.34426254,"Last":0.00008878,"BaseVolume":235.09663206,"TimeStamp":"2017-09-27T02:13:07.55","Bid":0.00008900,"Ask":0.00008982,"OpenBuyOrders":408,"OpenSellOrders":6009,"PrevDay":0.00008375,"Created":"2017-06-06T01:22:35.727"},
my current code just looks like this:
#!/usr/bin/python3
import urllib.request, json
from pprint import pprint
from json import encoder
encoder.FLOAT_REPR = lambda o: format(o, '.8f')
with urllib.request.urlopen("https://bittrex.com/api/v1.1/public/getmarketsummaries") as url:
data = json.loads(url.read().decode())
pprint (data)
that DOES NOT WORK even though my previous question was marked answered and a duplicate question of another question. That "duplicate" question had nothing to do with scientific notation and DID NOT fix the problem.
Upvotes: 1
Views: 4102
Reputation: 4652
Maybe, it will cause some problems, but they are easy to fix. You need to use json.dumps() instead of json.loads() Source
data = json.dumps(url.read().decode())
Output:
'"{\"success\":true,\"message\":\"\",\"result\":[{\"MarketName\":\"BTC-1ST\",\"High\":0.00009287,\"Low\":0.00008200,\"Volume\":1860886.81706592,\"Last\":0.00008800,\"BaseVolume\":163.34599977,\"TimeStamp\":\"2017-09-27T07:54:48.62\",\"Bid\":0.00008800,\"Ask\":0.00008818,\"OpenBuyOrders\":401,\"OpenSellOrders\":6015,\" ...
Upvotes: 2
Reputation:
Those values are already stored as floats! You can see this with the following code (modified from yours):
#!/usr/bin/python3
import urllib.request, json
from pprint import pprint
from json import encoder
encoder.FLOAT_REPR = lambda o: format(o, '.8f')
with urllib.request.urlopen("https://bittrex.com/api/v1.1/public/getmarketsummaries") as url:
data = json.loads(url.read().decode())
# pprint (data)
thing = list(filter(lambda x:x['MarketName']=='BTC-1ST', data['result']))[0]
pprint(thing['Ask'])
print('{:.10f}'.format(thing['Ask']))
print(type(thing['Ask']))
Notice how the first value from pprint
shows scientific notation (ie. the default string representation of that float value), while the second shows it formatted similar to how you are wanting it by forcing it to display that way.
So, if you're wanting to simply print out one of those values, you'll have to use string formatting as in my example, or if you want to pass those values to another program or function, you don't have to change anything since they are already floats!
Upvotes: 1