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
  Help with to check an Input list data with a data read from an external source sacharyya 3 402 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,429 Nov-09-2023, 10:56 AM
Last Post: mg24
Question Special Characters read-write Prisonfeed 1 609 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  How do I read and write a binary file in Python? blackears 6 6,503 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read text file, modify it then write back Pavel_47 5 1,587 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  Correctly read a malformed CSV file data klllmmm 2 1,926 Jan-25-2023, 04:12 PM
Last Post: klllmmm
  how to read txt file, and write into excel with multiply sheet jacklee26 14 9,899 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  Read nested data from JSON - Getting an error marlonbown 5 1,357 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Write sql data or CSV Data into parquet file mg24 2 2,418 Sep-26-2022, 08:21 AM
Last Post: ibreeden
  Read JSON via API and write to SQL database TecInfo 5 2,187 Aug-09-2022, 04:44 PM
Last Post: TecInfo

Forum Jump:

User Panel Messages

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