【问题标题】:How to load the XML files from directory please请如何从目录加载 XML 文件
【发布时间】:2023-03-21 01:41:01
【问题描述】:

这段代码是我在互联网上某处得到的,我对其进行了编辑。

如何从我的目录加载 XML 文件?有没有办法做到这一点?

from elementtree import ElementTree as et
# Load the xml content from a string
content = et.fromstring("C:\DATA\US_Patent_Data\2012\ipgb20120103_wk01\ipgb20120103.xml")


# Get the person or use the .findall method to get all
# people if there's more than person
applicant = content.find("applicant")
last_name = applicant.find("addressbook/last-name")
first_name = applicant.find("addressbook/first-name")

# Get the persons address
address = addressbook.find("address")
street = address.find("street")
city= address.find("city")
state =  address.find("state")
postcode = address.find("postcode")
country = address.find("country")

# Print output
print "sequence: " + applicant.attrib.get('sequence')
print "first name: " + first_name.text
print "last name: " + last_name.text
print "street: " + street.text
print "city: " + city.text
print "state: " + state.text
print "postcode: " + postcode.text
print "country: " + country.text

我运行了这个程序,这就是我得到的。 我复制了一部分...

  File "C:\Python27\lib\site-packages\elementtree\ElementTree.py", line 1292, in feed
self._parser.Parse(data, 0)

ExpatError: not well-formed (invalid token): line 1, column 2

【问题讨论】:

    标签: python xml elementtree xml.etree


    【解决方案1】:

    fromstring 函数用于从字符串中读取 xml 数据。

    要从文件中读取 xml 数据,您应该使用 parse。请参阅docs 使用 elementtree 解析 xml。

    import xml.etree.ElementTree as ET
    tree = ET.parse("C:\DATA\US_Patent_Data\2012\ipgb20120103_wk01\ipgb20120103.xml")
    root = tree.getroot()
    

    更新: 似乎您的 xml 格式不正确,因为它有多个根。尝试添加单个根元素:

    with open('ipgb20120103.xml', 'r') as f:
        xml_string = "<root>%s</root>" % f.read()
    
    root = ET.fromstring(xml_string)
    

    【讨论】:

    • 它说的是这样的。IOError: [Errno 2] No such file or directory: 'C:\\DATA\\US_Patent_Data\x812\\ipgb20120103_wk01\\ipgb20120103.xml' 每当我使用 etree.parse 时,我总是得到类似的东西
    • 2012 怎么能改成 x812 太奇怪了。我认为它改变了路径......我已经将文件移动到桌面,现在它给了我不同的错误File "C:\Python27\lib\site-packages\elementtree\ElementTree.py", line 1292, in feed self._parser.Parse(data, 0) ExpatError: junk after document element: line 414, column 0
    • XML 有更多的根,例如它有&lt;us-patent-grant&gt; 的重复,但只有在它关闭标签之后,它才会再次拥有 startTag...
    • 完整的错误是Traceback (most recent call last): File "myfilepath", line 3, in &lt;module&gt; tree = et.parse("ipgb20120103.xml") File "C:\Python27\lib\site-packages\elementtree\ElementTree.py", line 908, in parse tree = parser_api.parse(source) File "C:\Python27\lib\site-packages\elementtree\ElementTree.py", line 174, in parse parser.feed(data) File "C:\Python27\lib\site-packages\elementtree\ElementTree.py", line 1292, in feed self._parser.Parse(data, 0) ExpatError: junk after document element: line 414
    • 您认为是否有可能使用 Python 脚本添加单个根元素? @亚历山大
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-17
    • 2021-02-27
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多