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


Messages In This Thread
Write and read back data - by Aggie64 - Apr-17-2022, 09:01 PM
RE: Write and read back data - by Axel_Erfurt - Apr-17-2022, 09:13 PM
RE: Write and read back data - by bowlofred - Apr-17-2022, 09:16 PM
RE: Write and read back data - by Aggie64 - Apr-18-2022, 12:59 PM
RE: Write and read back data - by Aggie64 - Apr-18-2022, 01:00 PM
RE: Write and read back data - by Aggie64 - Apr-18-2022, 01:02 PM
RE: Write and read back data - by bowlofred - Apr-18-2022, 03:23 PM

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 419 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,475 Nov-09-2023, 10:56 AM
Last Post: mg24
Question Special Characters read-write Prisonfeed 1 634 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  How do I read and write a binary file in Python? blackears 6 6,704 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read text file, modify it then write back Pavel_47 5 1,636 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  Correctly read a malformed CSV file data klllmmm 2 1,972 Jan-25-2023, 04:12 PM
Last Post: klllmmm
  how to read txt file, and write into excel with multiply sheet jacklee26 14 10,052 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  Read nested data from JSON - Getting an error marlonbown 5 1,383 Nov-23-2022, 03:51 PM
Last Post: snippsat
  Write sql data or CSV Data into parquet file mg24 2 2,450 Sep-26-2022, 08:21 AM
Last Post: ibreeden
  Read JSON via API and write to SQL database TecInfo 5 2,219 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