XML parsing in python: expaterror not well-formed

前端 未结 3 1212
抹茶落季
抹茶落季 2021-01-08 01:09

I\'m using Python\'s xml.etree.ElementTree to do some XML parsing on a file. However, I get this error mid-way through the document:

xml.parsers         


        
相关标签:
3条回答
  • 2021-01-08 01:20

    I solve it by using yattag instead

    from yattag import indent
    print indent(xml_string.encode('utf-8'))
    
    0 讨论(0)
  • 2021-01-08 01:30

    The & is a special character in XML, used for character entities. If your XML has & sitting there by itself, not as part of an entity like & or ѐ or the like, then the XML is invalid.

    0 讨论(0)
  • 2021-01-08 01:39

    You can use the escape function found in the xml module

    from xml.sax.saxutils import escape
    
    my_string = "Some string with an &"
    
    # If the string contains &, <, or > they will be converted.
    print(escape(my_string))
    
    # Above will return: Some string with an &amp;
    

    Reference: Escaping strings for use in XML

    0 讨论(0)
提交回复
热议问题