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
  python to read the data from oracle melinda 3 1,663 Aug-18-2025, 10:24 AM
Last Post: Larz60+
  how to write/overwrite data in a txt. file according to inp Quinn 2 1,587 Aug-12-2025, 04:20 PM
Last Post: Quinn
  Write json data to csv Olive 6 2,436 Oct-22-2024, 06:59 AM
Last Post: Olive
  python read PDF Statement and write it into excel mg24 1 1,787 Sep-22-2024, 11:42 AM
Last Post: Pedroski55
  Delete file with read-only permission, but write permission to parent folder cubei 6 29,208 Jun-01-2024, 07:22 AM
Last Post: Eleanorreo
  Help with to check an Input list data with a data read from an external source sacharyya 3 2,661 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 5,857 Nov-09-2023, 10:56 AM
Last Post: mg24
Question Special Characters read-write Prisonfeed 1 2,140 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  How do I read and write a binary file in Python? blackears 6 35,976 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read text file, modify it then write back Pavel_47 5 6,983 Feb-18-2023, 02:49 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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