0

I am trying to come up with a way to delete a row in a CSV if it contains a certain value and not sure what structure is best to use?

I would like to read the file into memory and store it in a variable in a structure and then evaluate that structure and remove the desired rows.

Example:

aaa, 1, cat, car

bbb, 2, dog, truck

ccc, 1, bird, truck

ddd, 3, dog, car

eee, 3, mouse, car

fff, 2, cat, car

What I would want to do is remove the entire line if it contains a 2

So far I have tried placing it in a map, but I am not sure how to evaluate and remove the line. I would also be fine with simply not writing that line to the map.

class csvConsume {

    static void main(args) {
            def mapList = []

            File csvFile = new File("testData.csv")

            csvFile.eachLine { line ->
                def parts = line.split(",")
                def tmpMap = [:]

                tmpMap.putAt("FirstCol", parts[0])
                tmpMap.putAt("SecondCol", parts[1])
                tmpMap.putAt("ThirdCol", parts[2])
                tmpMap.putAt("FourthCol,", parts[3])


                // etc.

                mapList.add(tmpMap)

                print(tmpMap)


            }

    }
}
injecteer
  • 20,038
  • 4
  • 45
  • 89
Cylus
  • 11
  • 3

1 Answers1

0

yes you can!

def csvFile = '''
aaa, 1, cat, car
bbb, 2, dog, truck
ccc, 1, bird, truck
ddd, 3, dog, car
eee, 3, mouse, car
fff, 2, cat, car'''.trim()

def mapList = []

csvFile.splitEachLine( /,\s*/ ){ parts ->
    if( '2' != parts[ 1 ] ) mapList << [ FirstCol:parts[0], SecondCol:parts[1], ThirdCol:parts[2] ]
}

assert mapList*.SecondCol.contains( '1' )
assert !mapList*.SecondCol.contains( '2' )
assert mapList*.SecondCol.contains( '3' )
injecteer
  • 20,038
  • 4
  • 45
  • 89
  • Fantastic thank you Injecteer.. I have come across one issue with this solution the CSV file provided to me has some unfortunate characteristics.. inside it are some values like Smith,Bob,E within the CSV that are part of the same column of data.. When splitting on comma.. it ends up splitting the part as soon as it comes across a comma. – Cylus Oct 04 '19 at 19:54
  • @Cylus you should pay more attention to the splitting regexp. If the line parts are splitted comma + tab combo, you should be using `/,\t/` – injecteer Oct 04 '19 at 20:46
  • So upon further review, what they have done is this. eee, 3, "A sentence, with a comma in it.", car, dog. Issue I run into is it splits it and maps "A sentence, with a comma in it." Into two parts. – Cylus Oct 04 '19 at 21:54
  • splitting of the CSV string has nothing to do with your original question – injecteer Oct 06 '19 at 09:38