Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 930
Ramy Avatar asked Jul 19 '11 19:07

Ramy


1 Answers

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

f = open(workbook.name, "w")
f.write(soup.prettify())
f.close()
like image 130
Ken Avatar answered Sep 21 '22 15:09

Ken