MaryCoding
MaryCoding

Reputation: 664

Parsing CSV file and creating a key value pair

I am currently new and dabbling with groovy. I made a simple function called parseCsvFile. This function reads from a CSV file. However, when I try to println I don see the values but instead import au.com.bytecode.opencsv.CSVReader. My ultimate goal is to read the csv file and create a key value pair based in the data. How could I best accomplish that?

Function for parsing:

import au.com.bytecode.opencsv.CSVReader
import au.com.bytecode.opencsv.CSVParser

def parseCsvFile(String csvFilePath){
    if (fileExists(csvFilePath)) {
        new File(csvFilePath).withReader { reader ->
            CSVReader csvReader = new CSVReader(reader)
            csvReader.each { fields ->
                println fields
            }
        }
    } else {
        throw error
    }        

    //return a key value array
}

Example of csv:

GAME_ID,GAME_NAME,OLD_OWNER_NAME,NEW_OWNER_NAME,ORG_NAME
20001,str.git,Gemini,Kitoshi,Blue-DiamondGames
30001,str.git,Kashi,Sensu,FlyingMonkey

Upvotes: 1

Views: 3271

Answers (2)

Rao
Rao

Reputation: 21359

Since you are using groovy, you could simple use groovycsv and achieve the same with so simple as shown below:

Example - csv as String

@Grab('com.xlson.groovycsv:groovycsv:1.1')
import static com.xlson.groovycsv.CsvParser.parseCsv

def csv = """GAME_ID,GAME_NAME,OLD_OWNER_NAME,NEW_OWNER_NAME,ORG_NAME
20001,str.git,Gemini,Kitoshi,Blue-DiamondGames
30001,str.git,Kashi,Sensu,FlyingMonkey"""

def data = parseCsv(csv)
for(line in data) {
    println "$line.GAME_ID $line.GAME_NAME"
}

You can also use data from file. Just put the above data in csv file and provide input to FileReader and pass it to parseCsv method as shown below:

Example - csv as File

@Grab('com.xlson.groovycsv:groovycsv:1.1')
import static com.xlson.groovycsv.CsvParser.parseCsv
//Change the filepath as per your environment
def data = parseCsv(new FileReader('/tmp/game.csv'))
for(line in data) {
    println "$line.GAME_ID $line.GAME_NAME"
}

Note: You should be able to use column name to refer the data if you notice above println statement

Upvotes: 2

Vampire
Vampire

Reputation: 38629

Well, you should probably not use ancient versions of a library. In the version you are using (<= 2.3), the CSVReader is not an instance of Iterable, so the each method called is the one from Object which just iterates over the object itself. If you use a more recent version (>= 2.4) of the same library (package names and coordinates were refactored) it works like a charm.

Here an example:

@Grab('net.sf.opencsv:opencsv:2.3')
import au.com.bytecode.opencsv.CSVReader as opencsv23
@Grab('com.opencsv:opencsv:4.0')
import com.opencsv.CSVReader as opencsv40

new StringReader(
'''GAME_ID,GAME_NAME,OLD_OWNER_NAME,NEW_OWNER_NAME,ORG_NAME
20001,str.git,Gemini,Kitoshi,Blue-DiamondGames
30001,str.git,Kashi,Sensu,FlyingMonkey'''
).withReader { reader ->
    new opencsv23(reader).each { println it }
    println '========================'
    new opencsv40(reader).each { println it }
}

Which results in

au.com.bytecode.opencsv.CSVReader@31e9a763
========================
[GAME_ID, GAME_NAME, OLD_OWNER_NAME, NEW_OWNER_NAME, ORG_NAME]
[20001, str.git, Gemini, Kitoshi, Blue-DiamondGames]
[30001, str.git, Kashi, Sensu, FlyingMonkey]

Upvotes: 0

Related Questions