用美丽的汤写xml

时间:2011-07-19 19:55:28

标签: python beautifulsoup

这可能是一个真正愚蠢的问题,但我还没有找到答案。 一旦我根据需要修改了xml树,我该如何将其写回文件?

代码:

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

2 个答案:

答案 0 :(得分:13)

最简单的方法,将输出作为字符串并写入文件:

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

答案 1 :(得分:0)