Solved

Extract decimal from dictionary object of json data

Posted on 2015-01-14
1
91 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

770 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