Ramy
Ramy

Reputation: 21261

write xml with beautiful soup

this may be a truly stupid question but I haven't readily found the answer. once i modify the xml tree as necessary, how do I write it back out to file?

code:

workbook = open("C:\\Users\\rabdel.WINCMPT\\Documents\\Retail Footwear.twb")
soup = BeautifulSoup(workbook)

for dashboard in soup.findAll("dashboard"):
    print dashboard["name"]
    if dashboard["name"] == "S1":
        dashboard.extract()

for window in soup.findAll("window"):
    print "class:",window["class"]
    if "name" in [x[0] for x in window.attrs]:
        print "name:",window["name"]
        if window["name"] == "S1":
            window.extract()

Upvotes: 11

Views: 16314

Answers (2)

Ken
Ken

Reputation: 702

Simplest way, get the output as a string and write to file:

f = open(workbook.name, "w")
f.write(soup.prettify())
f.close()

Upvotes: 20

Related Questions