Parse a *.nfo file with python

喜欢而已 提交于 2019-12-11 12:03:44

问题


I try to parse a nfo file and print in a html code style (a table).
I tried with xml.etree but i get only 2 elements: Metadata and Category.
This is how a .nfo looks like:

<?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>

My code looks like:

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

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

But only print Category element, my question is how i can get values from Data?
Thanks!


回答1:


I'm guessing you might be looking to parse through the .nfo output from MSINFO32. The below code was what I found to be the most straightforward way to parse through the entire file, and come out with usable objects.

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

The output looks like:

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



回答2:


This works:

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

Output is:

Item : OS Name
Value : Microsoft Windows 8.1 Pro


来源:https://stackoverflow.com/questions/34070729/parse-a-nfo-file-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!