0

I have a CSV file like below :

enter image description here

I want to find a particular entry and retrieve the adjacent value of that entry.

For example, from the above example (screenshot), i want to retrieve the value adjacent to "upsert" which is 50.

Can i do this without using any external references/libraries (like openCSV etc.)?

user1523153
  • 139
  • 3
  • 10

1 Answers1

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] }
}

Please remember, that this will override result each time there is upset in operation. If you will have it more than once, just use list/map

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

Georgi Stoyanov
  • 469
  • 1
  • 5
  • 24
  • Can you also suggest me how can i convert CSV data into a list of map like : [Sno:"1", Service:"ProposalService",Operation:"upsert",ResponseTimeLimit:"50"], [Sno:"2",Service:"ScheduleService",Operation:"getReservation",ResponseTimeLimit:"10"], [Sno:"3",Service:"ScheduleService",Operation:"bookAppointment",ResponseTimeLimit:"23"] ] – user1523153 Apr 05 '18 at 19:09
  • You want list of maps. You could check answers here: https://stackoverflow.com/questions/18538/shortcut-for-creating-a-map-from-a-list-in-groovy/ Just need to create additional list with names of the columns and then apply one of the solutions there :) – Georgi Stoyanov Apr 06 '18 at 09:10