Getting the right formatting for my XML data

clemdcz

I am wondering why I'm not getting the right XML format when I'm writing my XML answer into a file.

Basically what I'm doing is sending a post request and receiving XML data back, doing something like this:

    post_request = s.post('myurl')
    soup2 = BeautifulSoup(post_request.text, features='xml')
    print(soup2.encode('utf-8')) 

    f=open("exit.xml","w")
    f.write(str(soup2.encode('utf-8')))
    f.close

First part does the work correctly but when I write it in my exit.xml all the data appears in the wrong format. What I mean by that is that everything is following, there is no indentation. Instead of going to the next line, there is \n between the data. I'm getting something like that:

<part number="1976">\n<name>Windscreen Wiper</name>\n<description>The Windscreen wiper automatically removes rain from your windscreen, if it should happen to splash there. It has a rubber\n <ref part="1977">blade</ref>\n which can be ordered separately if you need to replace it </description>\n </part>\n

Any ideas on how to fix?

Andrej Kesely

Try to add b flag when you open your file for writing. Also, remove the str(...):

with open('exit.xml', 'wb') as f_out:  # <-- add 'b' flag (binary)
    f_out.write(soup2.encode('utf-8'))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Getting right side numbers of my Id

Trouble getting the right website data into excel

Getting the conditional formatting into the HTMLBody

Am I formatting my input data optimally for an LSTM in Keras?

XSL - Only getting data from one node in my XML

Deserialize this XML File and getting data

Getting attribute from XML with my script

Pygame: Some of my sprites are getting stuck on the right side of the screen

How can I recover my data after formatting ubuntu with ubuntu?

Whats wrong with my mysql JOIN, I am not getting the right results

The right side of all the text on my website is getting cut off

Getting Specific Data from XML

Not getting data to transform Xml

PHP dynamic multidimensional arrays - getting data into the right format

I want to parse XML data into my android app, but it is not working right?

Data Not Getting Ordered BY in my Report

My recursion of getting subset doesn't print out the right answer

XML - getting data to go into a table (formatting)?

Getting Unique Values in XML Data

SQLite data not getting deleted in my RecyclerView

Carousel not displaying cards right after getting data from mysql

.xml URL without any formatting in the output data

Why my xml outpout file isn't formatting?

trouble formatting firebase Firestore data when getting for my flutter app

I'm having difficulty getting the right data into the matrices of my 4x3x21 array

Getting the right indentation of an XML file

Seeking the right token filters for my requirements and getting desperate

my elements are not getting shifted to right

Why is my unordered list getting pushed to the right side?