0% found this document useful (0 votes)
22 views3 pages

Weather Cloud Program

This Python script retrieves weather data from an online API by city name, writes it to a text file, then reads the data back from the file. It defines functions to output a header, get the weather description and temperature in Kelvin from the data, and convert Kelvin to Fahrenheit. The main code calls the API for Denver, CO, writes the response to a file, then reads it back and prints the output.

Uploaded by

Noah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

Weather Cloud Program

This Python script retrieves weather data from an online API by city name, writes it to a text file, then reads the data back from the file. It defines functions to output a header, get the weather description and temperature in Kelvin from the data, and convert Kelvin to Fahrenheit. The main code calls the API for Denver, CO, writes the response to a file, then reads it back and prints the output.

Uploaded by

Noah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

# Programmer : Nick Kounavelis

# Date : May 1, 2018


# Comment : Weather conditions by city name - JSON output

import urllib.request

# function definition - code


def convertKtoF(kelvin):
fahrenheit = (((kelvin - 273.14) * 1.8) + 32.0)
return fahrenheit

# function definition - code


def outputHeader():
print()
print("---------------------------------------------------------------------")
print("This is the weather string coming back from the cloud weather service")
print("---------------------------------------------------------------------")

# function definition - code


def getDescription(weatherData):
description=""
if weatherData.find("description") != -1:
tempOffset=weatherData.find("description")
tempOffset=tempOffset + 14
while weatherData[tempOffset] != '"':
description=description + weatherData[tempOffset]
tempOffset=tempOffset + 1

print("====================")
print("Weather conditions : ", description)
print("====================")

# function definition - code


def getTemperature(weatherData):
temperature=""
if weatherData.find("temp") != -1:
tempOffset=weatherData.find("temp")
tempOffset=tempOffset + 6
while weatherData[tempOffset] != ',':
temperature=temperature + weatherData[tempOffset]
tempOffset=tempOffset + 1

print("=======================")
print("Temperature in Kelvin : ", temperature)
print("=======================")

f_temp = convertKtoF(float(temperature))
print("=========================")
print("Temperature in Fahrenheit : ", format(f_temp, ".2f"))
print("=========================")

#
# main code starts here
#
city="Denver"
country="US"

# build the URL string


urlString = "http://api.openweathermap.org/data/2.5/weather?
q="+city+","+country+"&APPID=7ef1e1037f2e9bd211bd6d040743dee9"
webPage=urllib.request.urlopen(urlString)

#processing
outputHeader() # user-defined function - make a function call

for objline in webPage:


line = str(objline)
print(line)

#getDescription(line)
#getTemperature(line)

# write weather information to the disk


print()
print("----- Writing information to the hard disk -----")
outFile = open("weatherInfo.txt", "a")
outFile.write(line + "\r\n")
outFile.close()

# read data from the hard disk


try:
print()
print("----- Reading weather data from the hard disk -----")
print("---------------------------------------------------")
print("---------------------------------------------------")
readFile=open("weatherInfo.txt", "r")
line=readFile.readline()
while line != "":
print(line)
line=readFile.readline()
readFile.close()

except IOError:
print()
print("ERROR : IO error occurred!")
print()

#
# close the web page
#
print("Have a nice day!")
webPage.close()

You might also like