【问题标题】:write xml with beautiful soup用漂亮的汤写 xml
【发布时间】:2011-07-19 19:55:27
【问题描述】:

这可能是一个非常愚蠢的问题,但我还没有找到答案。 一旦我根据需要修改了 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()

【问题讨论】:

  • 你试过将 str(soup) 写入文件吗?

标签: python beautifulsoup


【解决方案1】:

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

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

【讨论】:

  • 或者,在某些情况下可能会更好 f.write(str(soup))
  • @AlexLelekov,的确,Graphpad Prism pzfx 与str() 一起使用,但不适用于.prettify() 输出
【解决方案2】:
猜你喜欢
  • 1970-01-01
  • 2017-08-24
  • 2016-08-13
  • 1970-01-01
  • 2015-11-19
  • 1970-01-01
  • 2019-07-16
  • 2019-11-10
  • 2013-03-21
相关资源
最近更新 更多