@ When ( "^I read info from csv file '(.+?)' with separator '(.+?)'$" ) public void readFromCSV ( String csvFile , String separator ) throws Exception { //By default separator is a coma char sep = ' ' ; if ( separator . length ( ) > 1 ) { switch ( separator ) { case "\\t" : sep = ' ' ; break ; default : sep = ' ' ; break ; } } else { sep = separator . charAt ( 0 ) ; } CsvReader rows = new CsvReader ( csvFile , sep ) ; String [ ] columns = null ; if ( rows . readRecord ( ) ) { columns = rows . getValues ( ) ; rows . setHeaders ( columns ) ; } List < Map < String , String > > results = new ArrayList < Map < String , String > > ( ) ; while ( rows . readRecord ( ) ) { Map < String , String > row = new HashMap < String , String > ( ) ; for ( String column : columns ) { row . put ( column , rows . get ( rows . getIndex ( column ) ) ) ; } results . add ( row ) ; } rows . close ( ) ; commonspec . setResultsType ( "csv" ) ; commonspec . setCSVResults ( results ) ; }
Read csv file and store result in list of maps