用python解析*.nfo文件

用python解析*.nfo文件,python,elementtree,msinfo32,Python,Elementtree,Msinfo32,我尝试解析一个nfo文件并以html代码样式(表格)打印。 我尝试了xml.etree,但只得到了两个元素:Metadata和Category 这就是.nfo的样子: <?xml version="1.0"?> <MsInfo> <Metadata> <Version>8.0</Version> <CreationUTC>12/02/15 10:45:25</CreationUTC> </Metadata&

我尝试解析一个nfo文件并以html代码样式(表格)打印。
我尝试了
xml.etree
,但只得到了两个元素:
Metadata
Category

这就是.nfo的样子:

<?xml version="1.0"?>
<MsInfo>
<Metadata>
<Version>8.0</Version>
<CreationUTC>12/02/15 10:45:25</CreationUTC>
</Metadata>
<Category name="System Summary">
<Data>
<Item><![CDATA[OS Name]]></Item>
<Value><![CDATA[Microsoft Windows 8.1 Pro]]></Value>
</Data>
</Category>
</MsInfo>
但只打印
类别元素
,我的问题是如何从
数据
中获取值?
谢谢

这是有效的:

for element in root.findall('Category'):
    value = element.find('Data')
    for child in value:
        print child.tag,":",child.text
输出为:

Item : OS Name
Value : Microsoft Windows 8.1 Pro
这项工作:

for element in root.findall('Category'):
    value = element.find('Data')
    for child in value:
        print child.tag,":",child.text
输出为:

Item : OS Name
Value : Microsoft Windows 8.1 Pro

我猜您可能希望通过MSINFO32的.nfo输出进行解析。我发现下面的代码是解析整个文件并生成可用对象的最直接的方法

for element in root.iter('Data'):
out = []
for n in range(len(element)):
    out.append('{0}'.format(element[n].text))
print(out)
输出如下所示:

['OS Name', 'Microsoft Windows 10 Enterprise Evaluation']
['Version', '10.0.15063 Build 15063']
['Other OS Description ', 'Not Available']
['OS Manufacturer', 'Microsoft Corporation']
['System Name', 'WIN10BLANK']
['System Manufacturer', 'Microsoft Corporation']
['System Model', 'Virtual Machine']
['System Type', 'x64-based PC']
['System SKU', 'Unsupported']

我猜您可能希望通过MSINFO32的.nfo输出进行解析。我发现下面的代码是解析整个文件并生成可用对象的最直接的方法

for element in root.iter('Data'):
out = []
for n in range(len(element)):
    out.append('{0}'.format(element[n].text))
print(out)
输出如下所示:

['OS Name', 'Microsoft Windows 10 Enterprise Evaluation']
['Version', '10.0.15063 Build 15063']
['Other OS Description ', 'Not Available']
['OS Manufacturer', 'Microsoft Corporation']
['System Name', 'WIN10BLANK']
['System Manufacturer', 'Microsoft Corporation']
['System Model', 'Virtual Machine']
['System Type', 'x64-based PC']
['System SKU', 'Unsupported']