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
Solved

Extract decimal from dictionary object of json data

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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 …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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…

860 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