Solved

Extract decimal from dictionary object of json data

Posted on 2015-01-14
1
90 Views
Last Modified: 2015-01-15
When extracting data from a dictionary object of json data I get the following error, it doesn't like the floating number i.e. 1.6, it just extracts integers. How do I adjust the script to parse the decimals.

Traceback (most recent call last):
  File "N:/Configuration/MetOfficehourlyjson test.py", line 30, in <module>
    Temperature = int((FCData_Dic['SiteRep']['DV']['Location']['Period'][0]['Rep'][0]['T']))
ValueError: invalid literal for int() with base 10: '1.6'


import urllib.request
import json

###  THIS IS THE CALL TO GET THE MET OFFICE FILE FROM THE INTERNET
response = urllib.request.urlopen('http://datapoint.metoffice.gov.uk/public/data/val/wxobs/all/json/3002?res=hourly&key=****API Key****')
FCData = response.read()
FCDataStr = FCData.decode('utf-8')
###   END OF THE CALL TO GET MET OFFICE FILE FROM THE INTERNET

#Converts JSON data to a dictionary object
FCData_Dic = json.loads(FCDataStr)

#The following are examples of extracting data from the dictionary object.
#The JSON data is heavily nested.
#Each [] goes one level down, usually defined with {} in the JSON data.
dataDate = (FCData_Dic['SiteRep']['DV']['dataDate'])
print('dataDate =',dataDate)

#There are also [] in the JSON data, which are referenced with integers, 
# starting from [0]
#Here, the [0] refers to the first day's block of data defined with [].
DateDay0 = (FCData_Dic['SiteRep']['DV']['Location']['Period'][0]['value'])
print('DateDay0 =',DateDay0)

#The second [0] picks out each of the first day's forecast data, in this case the time, referenced by '$'
TimeOfFC = (FCData_Dic['SiteRep']['DV']['Location']['Period'][0]['Rep'][0]['$'])
print('TimeOfFC =',TimeOfFC)

#Ditto for the temperature.    
Temperature = int((FCData_Dic['SiteRep']['DV']['Location']['Period'][0]['Rep'][0]['T']))
print('Temperature =',Temperature)

#Ditto for the screen relative humidity.    
#ScreenRelativeHumidity = int((FCData_Dic['SiteRep']['DV']['Location']['Period'][0]['Rep'][0]['H']))
#print('ScreenRelativeHumidity =',ScreenRelativeHumidity)

#Ditto for the weather Type (a code number).
#WeatherType = int((FCData_Dic['SiteRep']['DV']['Location']['Period'][0]['Rep'][0]['W']))
#print('WeatherType =',WeatherType)

#Ditto for the wind gust (a code number).
#WindGust = int((FCData_Dic['SiteRep']['DV']['Location']['Period'][0]['Rep'][0]['G']))
#print('WindGust =',WindGust)

#Ditto for the pressure.    
#Pressure = int((FCData_Dic['SiteRep']['DV']['Location']['Period'][0]['Rep'][0]['P']))
#print('Pressure =',Pressure)

Open in new window

0
Comment
Question by:crompnk
1 Comment
 
LVL 20

Accepted Solution

by:
Mark Brady earned 500 total points
ID: 40549064
On line 30 you are casting the value as an integer with int()

change it to this
Temperature = Decimal((FCData_Dic['SiteRep']['DV']['Location']['Period'][0]['Rep'][0]['T']))

Open in new window


make sure you import Decimal

from decimal import Decimal, getcontext

and set the context for the decimal

getcontext().prec = 6  # or whatever length you require
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
IronPython and Visual Studio 2013+ 2 79
Python frameworks - maybe Django. 4 85
python question 5 57
BASH script to modify crontab? 3 21
The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now