Groovy Split CSV

I have a csv file (details.csv) like

ID,NAME,ADDRESS
1,"{foo,bar}","{123,mainst,ny}"
2,"{abc,def}","{124,mainst,Va}"
3,"{pqr,xyz}","{125,mainst,IL}"

when I use (Note: I have another closure above this that reads all csv files from a directory)

if(file.getName().equalsIgnoreCase("deatails.csv")) {
 input = new FileInputStream(file)
 reader = new BufferedReader(new InputStreamReader(input))
 reader.eachLine{line-> def cols = line.split(",")
 println cols.size() }

Instead of getting size 3, I get 6 with values

1
"{foo
bar}"
"{123
mainst
ny}"

spilled (",") separates the data with a comma (,), but I want my results to be

1
"{foo,bar}"
"{123,mainst,ny}"

How can I fix this closure. Please help! Thanks

+5
source share
2 answers

Writing a csv parser is a complex business.

I would let someone do the hard work and use something like GroovyCsv


Here's how to parse it with GroovyCsv

// I'm using Grab instead of just adding the jar and its
// dependencies to the classpath
@Grab( 'com.xlson.groovycsv:groovycsv:1.0' )
import com.xlson.groovycsv.CsvParser

def csv = '''ID,NAME,ADDRESS
1,"{foo,bar}","{123,mainst,ny}"
2,"{abc,def}","{124,mainst,Va}"
3,"{pqr,xyz}","{125,mainst,IL}"'''

def csva = CsvParser.parseCsv( csv )
csva.each {
  println it
}

What prints:

ID: 1, NAME: {foo,bar}, ADDRESS: {123,mainst,ny}
ID: 2, NAME: {abc,def}, ADDRESS: {124,mainst,Va}
ID: 3, NAME: {pqr,xyz}, ADDRESS: {125,mainst,IL}

So, to get the NAME field of the second row, you can do:

def csvb = CsvParser.parseCsv( csv )
println csvb[ 1 ].NAME

What seal

{abc,def}

Of course, if the CSV is a file, you can do:

def csvc = new File( 'path/to/csv' ).withReader {
  CsvParser.parseCsv( it )
}

+20

.

def processCsvData(Map csvDataMap, File file)
{

    InputStream inputFile = new FileInputStream(file);
    String[] lines = inputFile.text.split('\n')
    List<String[]> rows = lines.collect {it.split(',')}
          // Add processing logic
}

({}) i.e "{foo, bar}" "{foo bar}" java,

public class CSVParser { 

    /* 
     * This Pattern will match on either quoted text or text between commas, including 
     * whitespace, and accounting for beginning and end of line. 
     */ 
    private final Pattern csvPattern = Pattern.compile("\"([^\"]*)\"|(?<=,|^)([^,]*)(?:,|$)");   
    private ArrayList<String> allMatches = null;         
    private Matcher matcher = null; 
    private int size; 

    public CSVParser() {                 
        allMatches = new ArrayList<String>(); 
        matcher = null; 
    } 

    public String[] parse(String csvLine) { 
        matcher = csvPattern.matcher(csvLine); 
        allMatches.clear(); 
        String match; 
        while (matcher.find()) { 
                match = matcher.group(1); 
                if (match!=null) { 
                        allMatches.add(match); 
                } 
                else { 
                        allMatches.add(matcher.group(2)); 
                } 
        } 

        size = allMatches.size();                
        if (size > 0) { 
                return allMatches.toArray(new String[size]); 
        } 
        else { 
                return new String[0]; 
        }                        
    }    

}

, !

0

All Articles