Solved

Extract decimal from dictionary object of json data

Posted on 2015-01-14
1
95 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
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 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…

688 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