python 尝试解析xml会抛出FileNotFoundError

mcvgt66p  于 2023-10-14  发布在  Python
关注(0)|答案(4)|浏览(112)

我是Python新手,我所做的只是解析一个简单的XML字符串。但是当我这样做的时候,它在Et.parse上说“没有这样的文件或目录”。我也试着说Et.parse(Et.fromstring(xmlfile)),但仍然导致了一个稍微不同的异常。

import xml.etree.ElementTree as ET
    
    
thisDict={}
    
def parseXML(xmlfile):
    tree = ET.parse(xmlfile)
    root=tree.getroot()
    for package in root.findall('package'):
    if package is None:
        continue
    if 'id' in package.attrib:
       thisDict['package']=package.get('id')
    if 'version' in package.attrib:
       thisDict['version']=package.get('version')

    for x, y in thisDict.items():
    print(x, y)

xmlstr=f'''<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="5.1.1" targetFramework="net481" />
<package id="Moq" version="4.18.4" targetFramework="net481" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net481" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net481" />
<package id="xunit" version="2.4.2" targetFramework="net481" />
<package id="xunit.abstractions" version="2.0.3" targetFramework="net481" />
<package id="xunit.analyzers" version="1.0.0" targetFramework="net481" />
<package id="xunit.assert" version="2.4.2" targetFramework="net481" />
<package id="xunit.core" version="2.4.2" targetFramework="net481" />
<package id="xunit.extensibility.core" version="2.4.2" targetFramework="net481" />
<package id="xunit.extensibility.execution" version="2.4.2" targetFramework="net481" />
<package id="xunit.runner.visualstudio" version="2.4.5" targetFramework="net481" developmentDependency="true" />
</packages>'''

    
parseXML(xmlstr)
nhhxz33t

nhhxz33t1#

因为xml是一个字符串,你应该使用ET.fromstring
对我来说,这是有效的,但这意味着你应该调整你的代码,不读取文件:

xml = '''<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="Castle.Core" version="5.1.1" targetFramework="net481" />
  <package id="Moq" version="4.18.4" targetFramework="net481" />
  <package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net481" />
  <package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net481" />
  <package id="xunit" version="2.4.2" targetFramework="net481" />
  <package id="xunit.abstractions" version="2.0.3" targetFramework="net481" />
  <package id="xunit.analyzers" version="1.0.0" targetFramework="net481" />
  <package id="xunit.assert" version="2.4.2" targetFramework="net481" />
  <package id="xunit.core" version="2.4.2" targetFramework="net481" />
  <package id="xunit.extensibility.core" version="2.4.2" targetFramework="net481" />
  <package id="xunit.extensibility.execution" version="2.4.2" targetFramework="net481" />
  <package id="xunit.runner.visualstudio" version="2.4.5" targetFramework="net481" developmentDependency="true" />
</packages>'''
tree = ET.fromstring(xml)
tree.findall('package')

如果你想使用你的函数,你可以使用io包:

import io

def parseXML(xmlfile):
    tree = ET.parse(xmlfile)
    root=tree.getroot()
    thisDict = {}
    for package in root.findall('package'):
        if package is None:
            continue
        if 'id' in package.attrib:
            thisDict['package']=package.get('id')
        if 'version' in package.attrib:
            thisDict['version']=package.get('version')

    for x, y in thisDict.items():
        print(x, y)

parseXML(io.StringIO(xml))
  • 注意我在你的函数中添加了thisDict = {}。但它可能不是你想要的,因为每次发现新的包时,'package'键都会被覆盖。
7y4bm7vi

7y4bm7vi2#

你可以用

root = ET.fromstring(xmlstr)

根据documentation
原因是当您使用tree = ET.parse(xmlfile)时,它搜索的是一个文件,而不是一个字符串

cqoc49vn

cqoc49vn3#

通过阅读文件导入此数据:

import xml.etree.ElementTree as ET
tree = ET.parse('country_data.xml')
root = tree.getroot()

或者直接从字符串中读取:

root = ET.fromstring(country_data_as_string)

你应该使用这个:

def parseXML(xmlfile):
    root = ET.fromstring(xmlfile)
    ...
dfty9e19

dfty9e194#

简单的方法是使用pandas:

import pandas as pd

xmlstr=f'''<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Castle.Core" version="5.1.1" targetFramework="net481" />
<package id="Moq" version="4.18.4" targetFramework="net481" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net481" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net481" />
<package id="xunit" version="2.4.2" targetFramework="net481" />
<package id="xunit.abstractions" version="2.0.3" targetFramework="net481" />
<package id="xunit.analyzers" version="1.0.0" targetFramework="net481" />
<package id="xunit.assert" version="2.4.2" targetFramework="net481" />
<package id="xunit.core" version="2.4.2" targetFramework="net481" />
<package id="xunit.extensibility.core" version="2.4.2" targetFramework="net481" />
<package id="xunit.extensibility.execution" version="2.4.2" targetFramework="net481" />
<package id="xunit.runner.visualstudio" version="2.4.5" targetFramework="net481" developmentDependency="true" />
</packages>'''

df = pd.read_xml(xmlstr, xpath = './/*')
print(df.to_string(index=False))

输出量:

id version targetFramework developmentDependency
                           Castle.Core   5.1.1          net481                  None
                                   Moq  4.18.4          net481                  None
System.Runtime.CompilerServices.Unsafe   4.5.3          net481                  None
     System.Threading.Tasks.Extensions   4.5.4          net481                  None
                                 xunit   2.4.2          net481                  None
                    xunit.abstractions   2.0.3          net481                  None
                       xunit.analyzers   1.0.0          net481                  None
                          xunit.assert   2.4.2          net481                  None
                            xunit.core   2.4.2          net481                  None
              xunit.extensibility.core   2.4.2          net481                  None
         xunit.extensibility.execution   2.4.2          net481                  None
             xunit.runner.visualstudio   2.4.5          net481                  true

Pandas也有一个用于dict()的嵌套结构:

d = df.to_dict('records')
print(d)

输出量:

[{'id': 'Castle.Core', 'version': '5.1.1', 'targetFramework': 'net481', 'developmentDependency': None}, {'id': 'Moq', 'version': '4.18.4', 'targetFramework': 'net481', 'developmentDependency': None}, {'id': 'System.Runtime.CompilerServices.Unsafe', 'version': '4.5.3', 'targetFramework': 'net481', 'developmentDependency': None}, {'id': 'System.Threading.Tasks.Extensions', 'version': '4.5.4', 'targetFramework': 'net481', 'developmentDependency': None}, {'id': 'xunit', 'version': '2.4.2', 'targetFramework': 'net481', 'developmentDependency': None}, {'id': 'xunit.abstractions', 'version': '2.0.3', 'targetFramework': 'net481', 'developmentDependency': None}, {'id': 'xunit.analyzers', 'version': '1.0.0', 'targetFramework': 'net481', 'developmentDependency': None}, {'id': 'xunit.assert', 'version': '2.4.2', 'targetFramework': 'net481', 'developmentDependency': None}, {'id': 'xunit.core', 'version': '2.4.2', 'targetFramework': 'net481', 'developmentDependency': None}, {'id': 'xunit.extensibility.core', 'version': '2.4.2', 'targetFramework': 'net481', 'developmentDependency': None}, {'id': 'xunit.extensibility.execution', 'version': '2.4.2', 'targetFramework': 'net481', 'developmentDependency': None}, {'id': 'xunit.runner.visualstudio', 'version': '2.4.5', 'targetFramework': 'net481', 'developmentDependency': 'true'}]

相关问题