Groovy阅读文本文件,但省略标题 [英] Groovy read text file but omit header
问题描述
这是后续问题的问题: Groovy解析文本文件
p>现在的区别是我的文件有一个标题,我试图先读过标题,然后转到我想要的内容上,但出于某种原因,
def dataList = []
def theInfoName ='testdata.txt'
布尔headersDone = false //标题默认为false
如果(!theInfoFile.exists()){
println文件theInfoFile = new File(theInfoName)
文件不存在
else {
def driveInfo = [:]
//遍历文件中的每一行
theInfoFile.eachLine {line - >
//这是我试图说明头部
if(!headersDone){//如果线条包含...,如果它将headerDone变为true
if(line.contains(...)){
headersDone = true
}
} else {
//如果该行不为空
if(line.trim()){
//分解为一个键值
def(key,value)= line.split('\ t:').collect {it .trim()}
//并将它们存储在driveInfo Map中
driveInfo。$ key= value
}
else {
//如果行是空白的,并且我们有一些信息
if(driveInfo){
//将它存储在列表中
dataList<<< driveInfo
//并清除它
driveInfo = [:]
}
}
}
//当我们完成文件时,存储任何其余数据
if(driveInfo){
dataList<< driveInfo
}
}
dataList.eachWithIndex {it,index - >
printlnDrive $ index
it.each {k,v - >
println\ t $ k = $ v
}
}
我试用了上一篇文章中提供的代码,以确保它不是我做的不同的事情,它的输出相同。
会发生什么,它发布相同的信息块11次。
标题看起来如下:
随机日期信息以及其他信息
稍微更随机的信息后跟
检查硬盘信息...
HDD设备0:/ dev / sda
硬盘型号:ST3160815A
硬盘序列号:5RA020QY
硬盘版本:3.AAA
硬盘大小:152628 MB
接口:IDE / ATA
温度:33 C
健康状况:100%
表现:70%
开机时间:27天,13小时
Est。使用寿命:超过1000天
硬盘设备1:/ dev / sdb
硬盘型号:TOSHIBA MK1237GSX
硬盘序列号:97LVF9MHS
硬盘版本:DL130M
硬盘大小:114473 MB
接口:S-ATA
温度:30 C
健康:100%
性能:100%
开机时间:38天,11小时
Est。终身:超过1000天
有人知道为什么要打印出重复的信息吗?
问题在于添加了last driveInfo
dataList
:
//当我们完成文件时,存储任何剩余的数据
if(driveInfo){
dataList<< driveInfo
}
它必须是一个大括号,否则它属于它到每条线关闭。
This is a follow up to the question asked here: Groovy parsing text file
The difference now is my file has a header, I attempted to read past the header first and then onto the content that I am wanting, but for some reason it doesn't seem to cooperate.
def dataList = []
def theInfoName = 'testdata.txt'
boolean headersDone = false //header set to false by default
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 ->
//this is where im trying to account for the header
if(!headersDone) { //look if line contains "..." if it does that turn headersDone to true
if(line.contains("...")) {
headersDone = true
}
} else {
// 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"
}
}
I tried it out with the code provided in the previous post to make sure it wasn't something I was doing differently and it came with the same output.
What happens is that it posts the same block of information 11 times.
The header looks is the following:
Random date information here with some other info
Slightly more random information followed by
Examining hard disk information ...
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
Does anyone know why it is printing out duplicate of the information?
The problem is the addition of the "last" driveInfo
to the dataList
:
// when we've finished the file, store any remaining data
if( driveInfo ) {
dataList << driveInfo
}
It has to be one curly brace below its current position, otherwise it belongs to the eachLine closure.
这篇关于Groovy阅读文本文件,但省略标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!