我写了一个脚本,使用tokenize在groovy中解析.csv文件,结果并不完全符合我的需要,我正在尝试使用openCSV库,但我不确定如何解析出单独的列。到目前为止,我的代码如下:
List<String[]> rows = new CSVReader(
new InputStreamReader(getClass().classLoader.getResourceAsStream(inputFileString)))
.readAll()
rows.each { row ->
row.each { it ->
println it
}
}下面是我的输入数据:
1,"unknown","positive","full message","I love it."所以我想弄明白的是如何打印行中的select列。同样感谢提前,我正在尝试了解groovy/java,我来自Ruby背景。
发布于 2012-06-15 16:54:06
不知道你所说的'...how to print select column in to row‘是什么意思
但此脚本(例如)打印每行的第4列:
@Grab( 'net.sf.opencsv:opencsv:2.3' )
import au.com.bytecode.opencsv.CSVReader
// This sets example to a 2 line string
// I'm using it instead of a file, as it makes
// an easier example to follow
def example = '''1,"unknown","positive","full message","I love it."
|2,"tim","negative","whoop!","It's ok"'''.stripMargin()
List<String[]> rows = new CSVReader( new StringReader( example ) ).readAll()
rows.each {
// print the 4th column
println it[ 3 ]
}这将打印:
full message
whoop!https://stackoverflow.com/questions/11042644
复制相似问题