The code shown below does the following:
1-gets an XML from the internet-this works
2-save the data to a disk file -this works
3-reads back the data from disk -this does not work. There no errors, but the data is None instead of containing the XML data which is about 93k in size.
The following is the code:
#1. this codes reads weather data using list of airport identifiers to retrieve their current weather using the URL shown below.
the weather is contained in an XML file that is successfully retrieved from the URL and urllib.request
#2. the XML file is written to disk successfully
#3 read the XML data back from disk and the read works without error, but the variable content is now None instead of containing the XML data
The following is the source code:
import urllib.request
#read list of airports from disk and load into "airports"
airportsfilename = "/home/pi/user_wallis_99airports"
try:
with open(airportsfilename) as f:
airports = f.readlines() # used in the url = line of code
airports = [x.strip() for x in airports]
#print("1412 airports = ",airports)
except IOError:
print(" User airports file not found")
airports = None
#1 retrieve the XML file - this is successful
#get the content of an XML file
#the following code reads airport weather from the url below and uses "airports" as a list of airports to use to get their current weather data
url = "https://www.aviationweather.gov/adds/dataserver_current/httpparam?dataSource=metars&requestType=retrieve&format=xml&hoursBeforeNow=5&mostRecentForEachStation=true&stationString=" + ",".join([item[:4] for item in airports if item != "NULL"])
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36 Edg/86.0.622.69'})
content=None
#get the content of an XML file
while content is None: #Loop, if necessary to read METAR data from FAA server
try:
content = urllib.request.urlopen(req).read()
print("2986 successful read of METAR data")
except:
print("Error")
print("1-print content written to disk content = ",content)
#-----------------------------------------------------------
print("============================")
#------------------------------------------------------
#2 Save XML content to disk in a file named "savecontent" - this isuccessful
#------------------------------------------------------
try:
with open("/home/pi/metarhistory/savecontent","wb") as f:
f.write(content)
print("Save content to disk")
except IOError:
print("IOError writing content")
print("2-print content",content)
#at this point, the XML data is saved on disk in a file named "savecontent" which is about 93k in size.
#------------------------------------------------------
#3 read content back from disk this does not work, the variable content is None
#------------------------------------------------------
content=None
try:
with open("/home/pi/metarhistory/savecontent","rb") as f:
f.read(content)
except IOError:
print("IOError reading content from disk")
print("3-Read content from disk content = ",content) #content is now equal to None instead of containing the XML data
print("End of Program")