Groovy Split CSV

后端 未结 2 961
半阙折子戏
半阙折子戏 2020-12-16 07:16

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         


        
相关标签:
2条回答
  • 2020-12-16 07:24

    Writing a csv parser is a tricky business.

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


    Here is 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
    }
    

    Which 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 could do:

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

    Which prints

    {abc,def}
    

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

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

    Then use it as above

    0 讨论(0)
  • 2020-12-16 07:36

    There are two ways of doing. One is using collect

    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
    }
    

    Here problem is it is removing commas in between braces ({}) i.e "{foo,bar}" becomes "{foo bar}" Another way of using java, and this works just fine

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

    Hope this helps!

    0 讨论(0)
提交回复
热议问题