Problem with newlines when I use toprettyxml()

前端 未结 8 885
不知归路
不知归路 2020-11-28 15:17

I\'m currently using the toprettyxml() function of the xml.dom module in a Python script and I\'m having some trouble with the newlines. If don\'t

8条回答
  •  臣服心动
    2020-11-28 15:51

    I found another great solution :

    f = open(filename, 'w')
    dom_string = dom1.toprettyxml(encoding='UTF-8')
    dom_string = os.linesep.join([s for s in dom_string.splitlines() if s.strip()])
    f.write(dom_string)
    f.close()
    

    Above solution basically removes the unwanted newlines from the dom_string which are generated by toprettyxml().

    Inputs taken from -> What's a quick one-liner to remove empty lines from a python string?

提交回复
热议问题