开发者

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()


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

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


From the docs: you can turn a Beautiful Soup document (or any subset of it) into a string with the str function, or the prettify or renderContents methods. You can also use the unicode function to get the whole document as a Unicode string.

Then just write that string to a file as you would any other string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜