Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Write and read back data
#1
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.
Reply
#2
https://www.w3schools.com/python/ref_func_open.asp

"r" - Read

"a" - Append

"w" - Write
Reply
#3
(Apr-17-2022, 09:01 PM)Aggie64 Wrote: I get an error on the write that the data must be str, not bytes

So it may be ascii (or utf-8) inside, but hasn't been decoded to that. In which case, you probably want to write and read it as binary.
with open("savedata", "wb") as f:
    f.write(content)

with open("savedata", "rb") as f:
    content = f.read()
Reply
#4
Your code worked!!! Many thanks.

I don't understand the difference in f.write(content) vs content=f.read().
I need to do more reading.
Thanks again Smile
Reply
#5
(Apr-18-2022, 12:59 PM)Aggie64 Wrote: Your code worked!!! Many thanks.

I don't understand the difference in f.write(content) vs content=f.read().
I need to do more reading.
Thanks again Smile
Reply
#6
I meant to say I don't understand the difference in f.read(content) vs content=f.read().
Reply
#7
The only argument that read() takes is an optional maximum size of characters to read. And it returns the data, so you'll want to assign it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Write sql data or CSV Data into parquet file mg24 2 102 Sep-26-2022, 08:21 AM
Last Post: ibreeden
  Read JSON via API and write to SQL database TecInfo 5 313 Aug-09-2022, 04:44 PM
Last Post: TecInfo
  Read data via bluetooth frohr 9 627 Jul-10-2022, 09:51 AM
Last Post: frohr
  Is this possible to write a script for checking data from website? WanW 2 443 Jun-02-2022, 02:31 AM
Last Post: Larz60+
  How to read rainfall time series and insert missing data points MadsM 4 991 Jan-06-2022, 10:39 AM
Last Post: amdi40
  Strategy on updating edits back to data table and object variables hammer 0 649 Dec-11-2021, 02:58 PM
Last Post: hammer
  UART Serial Read & Write to MP3 Player Doesn't Work bill_z 15 3,177 Jul-17-2021, 04:19 PM
Last Post: bill_z
  Read and write active Excel file euras 4 1,662 Jun-29-2021, 11:16 PM
Last Post: Pedroski55
Lightbulb [Solved] df.loc: write data in certain rows ju21878436312 1 1,095 Jun-28-2021, 06:49 AM
Last Post: ju21878436312
  [Solved] Using readlines to read data file and sum columns Laplace12 4 1,774 Jun-16-2021, 12:46 PM
Last Post: Laplace12

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020