嗨我有一个输入文件,我需要输出。我也在尝试java代码和Groovy代码,但无法找到结果。如果有人可以提供帮助,请告诉我。输入是一个txt文件,所有值只在一列中。
输入:
A B C D E
F G H I
J K L M
N O P Q R
S T U V
W X Y Z
输出:
A B C D E
A F G H I
A J K L M
N O P Q R
N S T U V
N W X Y Z
这是我到目前为止所尝试的:
data['R1']= R.toString()
for (int i=0; i<R.length(); i++) {
if (i==1) R['i']= 'A';
if (i==2) R[i] = 'A';
if (i==4) R[i] ='S';
if (i ==5) R['i'] = 'S';
}
println R
答案 0 :(得分:0)
这是Groovy中的一个例子
// Mock the input file
def input = """A B C D E
F G H I
J K L M
N O P Q R
S T U V
W X Y Z"""
def inputFile = new StringReader(input)
// Process the input file
inputFile
.iterator()
.collect { it.split(" ") } // Break each line into columns.
.inject([keep : null, rows : []]) { context, row ->
if(row[0]) {
/*
* If the row contains a value in the first column,
* remember the value and keep the row as-is.
*/
[
keep : row[0],
rows : context.rows + row
]
} else {
/*
* If the row doesn't have a value in the first column,
* prepend the remembered value to the row.
*/
[
keep : context.keep,
rows : context.rows << [context.keep] + row.drop(2).flatten()
]
}
}
.rows // The context is no longer needed, so just grab the rows form it.