Python ElementTree parsing unbound prefix error

前端 未结 2 1834
长发绾君心
长发绾君心 2021-01-07 19:01

I am learning ElementTree in python. Everything seems fine except when I try to parse the xml file with prefix:

test.xml:



        
相关标签:
2条回答
  • 2021-01-07 19:20

    See if this works:

    from bs4 import BeautifulSoup
    
    xml_file = "test.xml"
    
    with open(xml_file, "r", encoding="utf8") as f:
        contents = f.read()
        soup = BeautifulSoup(contents, "xml")
    
        items = soup.find_all("country")
        print (items)
    

    The above will produce an array which you can then manipulate to achieve your aim (e.g. remove html tags etc.):

    [<country name="Liechtenstein" rank="1" year="2008">
    </country>, <country name="Singapore" rank="4" year="2011">
    </country>, <country name="Panama" rank="5" year="2011">
    </country>]
    
    0 讨论(0)
  • 2021-01-07 19:34

    Add the abc namespace to your xml file.

    <?xml version="1.0"?>
    <abc:data xmlns:abc="your namespace">
    
    0 讨论(0)
提交回复
热议问题