使用python解析* .nfo文件 [英] Parse a *.nfo file with python

查看:514
本文介绍了使用python解析* .nfo文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试解析nfo文件并以html代码样式(表)进行打印.
我尝试使用xml.etree,但只有2个元素:MetadataCategory.
.nfo的外观如下:

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>

我的代码如下:

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

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

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

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

推荐答案

我猜您可能正在解析MSINFO32的.nfo输出.下面的代码是我发现解析整个文件并提供可用对象的最直接方法.

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)

输出看起来像:

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

这篇关于使用python解析* .nfo文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆