用美丽的汤写xml

Ram*_*amy 6 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()
Run Code Online (Sandbox Code Playgroud)

小智 14

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

f = open(workbook.name, "w")
f.write(soup.prettify())
f.close()
Run Code Online (Sandbox Code Playgroud)