使用 python 解析 *.nfo 文件

Parse a *.nfo file with python

我尝试解析 nfo 文件并以 html 代码样式(table)打印。
我尝试使用 xml.etree 但我只得到 2 个元素:MetadataCategory.
这是 .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>

我的代码如下:

tree = ET.parse(File)
root = tree.getroot()

for element in root.findall('Category'):
    value = element.find('Data')
    print element.attrib

但只打印 Category element,我的问题是如何从 Data?
获取值 谢谢!

这个有效:

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']