如何从.nfo文件中解析/提取数据?
我有很多带有服务器信息的.nfo文件。我需要从每个.nfo文件中收集各种信息。例如,我需要列出:
如何将此信息解析为例如excel或文本文件?由于我有> 150 .nfo文件,我不想打开每个文件并复制/过去。
希望你能帮助我。
答案 0 :(得分:1)
我猜您可能正在解析MSINFO32的.nfo输出。下面的代码是我发现解析整个文件最简单的方法,并提供了易于管理的列表。这使用ElementTree:
https://docs.python.org/3/library/xml.etree.elementtree.html
import xml.etree.ElementTree as ET
tree = ET.parse('MSINFO32.nfo')
root = tree.getroot()
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']
答案 1 :(得分:0)
我没有Data types
打印其他内容。msinfo32
将其报告打印为unicode xml文件,因此如果首先将它们转换为ascii,它们可以被解析为文本文件:
(如果所有.nfo
个文件都在同一目录中)
@echo off
setlocal enableDelayedExpansion
::OS Name
::Processor
::Total Physical Memory
::Drive
::Size
set "line1="
set "line2="
for %%X in (*.nfo) do (
echo+
echo ---%%X---
echo+
type "%%~fX" > "%temp%\%%~nxX"
rem exit /b 0
for /f "usebackq tokens=3 delims=[]" %%# in ("%temp%\%%~nxX") do (
rem echo %%#
set "line2=!line1!"
set "line1=%%#"
rem echo !line1!
rem echo !line1! !line2!
if "!line2!" equ "Size" (
echo !line2! - !line1!
)
if "!line2!" equ "Drive" (
echo !line2! - !line1!
)
if "!line2!" equ "Total Physical Memory" (
echo !line2! - !line1!
)
if "!line2!" equ "OS Name" (
echo !line2! - !line1!
)
if "!line2!" equ "Processor" (
echo !line2! - !line1!
)
)
del /q /f "%temp%\%%~nxX" >nul 2>&1
)