Apr-17-2022, 09:01 PM
I have a working Python 3 program that reads XML data from a URL and processes the data received
The code shown below is how the data is received
req= urllib.Request(url,headers=.....)
content = urllib.request.urlopen(req).read #req comes from a statement
root=ET.fromstring(content)
for metar in root.iter('METAR')
#the code in the for statement process the metar data
I want to be able to save the data in the variable "content" or "root" to a file and then later read the data back just as though I had received it from the URL and then process the data the same way as is done when read from a URL.
I tried the following:
with open("savedata",'r+',encoding='utf-8') as f:
f.write(content)
with open('savedata','r+',encoding='utf-8') as f:
f.read(content)
I get an error on the write that the data must be str, not bytes
I believe the data in the variable "content" is all ascii data.
an example of the data in the variable "content" starts out as follows:
<?xml version ="1.0 encoding ='UTF-8"?>\n<response xmlns:xsd="http...
Thanks, in advance for any help.
The code shown below is how the data is received
req= urllib.Request(url,headers=.....)
content = urllib.request.urlopen(req).read #req comes from a statement
root=ET.fromstring(content)
for metar in root.iter('METAR')
#the code in the for statement process the metar data
I want to be able to save the data in the variable "content" or "root" to a file and then later read the data back just as though I had received it from the URL and then process the data the same way as is done when read from a URL.
I tried the following:
with open("savedata",'r+',encoding='utf-8') as f:
f.write(content)
with open('savedata','r+',encoding='utf-8') as f:
f.read(content)
I get an error on the write that the data must be str, not bytes
I believe the data in the variable "content" is all ascii data.
an example of the data in the variable "content" starts out as follows:
<?xml version ="1.0 encoding ='UTF-8"?>\n<response xmlns:xsd="http...
Thanks, in advance for any help.