如何使用Groovy

时间:2018-04-04 18:08:15

标签: groovy

我有一个如下所示的CSV文件:

enter image description here

我想查找特定条目并检索该条目的相邻值。

例如,从上面的示例(屏幕截图)中,我想检索与" upsert"相邻的值。这是50。

我可以不使用任何外部引用/库(如openCSV等)吗?

1 个答案:

答案 0 :(得分:1)

//load and split the file
InputStream inputFile = getClass().classLoader.getResourceAsStream(TEST_FILE_NAME)
String[] lines = inputFile.text.split('\n')
List<String[]> rows = lines.collect {it.split(',')}

private String OPERATION = 2;
private String RESPONSE_TIME_LIMIT = 3;
private int result;

rows.each { row ->
            String operationValue = row[OPERATION]
            if(operationValue == "upset") { result = row[RESPONSE_TIME_LIMIT] }
}

请记住,每次操作失败时,这都会覆盖结果。如果你不止一次,只需使用list / map

来源:http://www.kellyrob99.com/blog/2010/07/01/groovy-and-csv-how-to-get-your-data-out/