Remove whitespaces in XML string

前端 未结 8 1734
我在风中等你
我在风中等你 2020-11-29 04:53

How can I remove the whitespaces and line breaks in an XML string in Python 2.6? I tried the following packages:

etree: This snippet keeps the original whitespaces:<

8条回答
  •  再見小時候
    2020-11-29 05:28

    A little clumsy solution without lxml :-)

    data = """
    
            
    
    """
    
    data3 = []
    data2 = data.split('\n')
    for x in data2:
        y = x.strip()
        if y: data3.append(y)
    data4 = ''.join(data3)
    data5 = data4.replace("  ","").replace("> <","><")
    
    print data5
    
    Output: 
    

提交回复
热议问题