gpt4 book ai didi

file - Groovy 解析文本文件

转载 作者:行者123 更新时间:2023-12-04 01:33:03 28 4
gpt4 key购买 nike

我有一个我想解析的文件日志,但遇到了一些问题。起初,这似乎很简单。我会继续发布我想出的来源,然后解释我想要做什么。

我试图解析的文件包含以下数据:

HDD Device 0 : /dev/sda
HDD Model ID : ST3160815A
HDD Serial No : 5RA020QY
HDD Revision : 3.AAA
HDD Size : 152628 MB
Interface : IDE/ATA
Temperature : 33 C
Health : 100%
Performance : 70%
Power on Time : 27 days, 13 hours
Est. Lifetime : more than 1000 days

HDD Device 1 : /dev/sdb
HDD Model ID : TOSHIBA MK1237GSX
HDD Serial No : 97LVF9MHS
HDD Revision : DL130M
HDD Size : 114473 MB
Interface : S-ATA
Temperature : 30 C
Health : 100%
Performance : 100%
Power on Time : 38 days, 11 hours
Est. Lifetime : more than 1000 days

我的源代码(如下)基本上将文件逐行分解,然后将该行分成两部分(键:值)。

来源:
def dataList = [:]
def theInfoName = "C:\\testdata.txt"

File theInfoFile = new File(theInfoName)

def words
def key
def value

if (!theInfoFile.exists()) {
println "File does not exist"

} else {

theInfoFile.eachLine { line ->

if (line.trim().size() == 0) {
return null

} else {

words = line.split("\t: ")
key=words[0]
value=words[1]
dataList[key]=value

println "${words[0]}=${words[1]}"
}

}
println "$dataList.Performance" //test if Performance has over-written the previous Performance value
}

我的来源的问题是,当我使用我的 getter(例如 $dataList.Performance)时,它只显示文件中的最后一个而不是两个。

所以我想知道,我如何解析文件以保留两个硬盘驱动器的信息?有没有办法将信息打包到“硬盘驱动器对象”中?

任何和所有帮助表示赞赏

一些旁注:

该文件在 Windows 机器上(即使信息是从 nix 系统中获取的)

文本文件由制表符、冒号和空格(如我的源代码中所示)分割,只是我想我会说,因为它在此页面上看起来不像。

最佳答案

这将读取块中的数据(用空行分隔块)

def dataList = []
def theInfoName = 'testdata.txt'

File theInfoFile = new File( theInfoName )

if( !theInfoFile.exists() ) {
println "File does not exist"
} else {
def driveInfo = [:]
// Step through each line in the file
theInfoFile.eachLine { line ->
// If the line isn't blank
if( line.trim() ) {
// Split into a key and value
def (key,value) = line.split( '\t: ' ).collect { it.trim() }
// and store them in the driveInfo Map
driveInfo."$key" = value
}
else {
// If the line is blank, and we have some info
if( driveInfo ) {
// store it in the list
dataList << driveInfo
// and clear it
driveInfo = [:]
}
}
}
// when we've finished the file, store any remaining data
if( driveInfo ) {
dataList << driveInfo
}
}

dataList.eachWithIndex { it, index ->
println "Drive $index"
it.each { k, v ->
println "\t$k = $v"
}
}

手指交叉,您的硬盘信息部分之间有空行(您在测试数据中显示了一个):-)

顺便说一句:我得到以下输出:
Drive 0
HDD Device 0 = /dev/sda
HDD Model ID = ST3160815A
HDD Serial No = 5RA020QY
HDD Revision = 3.AAA
HDD Size = 152628 MB
Interface = IDE/ATA
Temperature = 33 C
Health = 100%
Performance = 70%
Power on Time = 27 days, 13 hours
Est. Lifetime = more than 1000 days
Drive 1
HDD Device 1 = /dev/sdb
HDD Model ID = TOSHIBA MK1237GSX
HDD Serial No = 97LVF9MHS
HDD Revision = DL130M
HDD Size = 114473 MB
Interface = S-ATA
Temperature = 30 C
Health = 100%
Performance = 100%
Power on Time = 38 days, 11 hours
Est. Lifetime = more than 1000 days

搞砸了,我也把代码归结为:
def dataList = []
def theInfoFile = new File( 'testdata.txt' )

if( !theInfoFile.exists() ) {
println "File does not exist"
} else {
// Split the text of the file into blocks separated by \n\n
// Then, starting with an empty list go through each block of text in turn
dataList = theInfoFile.text.split( '\n\n' ).inject( [] ) { list, block ->
// Split the current block into lines (based on the newline char)
// Then starting with an empty map, go through each line in turn
// when done, add this map to the list we created in the line above
list << block.split( '\n' ).inject( [:] ) { map, line ->
// Split the line up into a key and a value (trimming each element)
def (key,value) = line.split( '\t: ' ).collect { it.trim() }
// Then, add this key:value mapping to the map we created 2 lines above
map << [ (key): value ] // The leftShift operator also returns the map
// the inject closure has to return the accumulated
// state each time the closure is called
}
}
}

dataList.eachWithIndex { it, index ->
println "Drive $index"
it.each { k, v ->
println "\t$k = $v"
}
}

但这必须一次将整个文件加载到内存中(并依赖于 \n 作为 EOL 终止字符)

关于file - Groovy 解析文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3360191/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com