Groovy Split CSV

时间:2012-08-16 20:45:17

标签: csv groovy split

我有一个csv文件(details.csv),如

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

当我使用时(注意:我在其上方有其他闭包,它从目录中读取所有csv文件)

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() }

而不是获得大小3,我得到的值是6

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

spilled(“,”)用逗号(,)分割数据,但我希望我的结果为

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

如何修复此闭包。请帮忙!感谢

2 个答案:

答案 0 :(得分:21)

编写csv解析器是一项棘手的工作。

我会让其他人做这项艰苦的工作,并使用like GroovyCsv


以下是如何使用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
}

打印哪些:

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}

因此,要获取第二行的NAME字段,您可以执行以下操作:

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

打印

{abc,def}

当然,如果CSV是文件,您可以这样做:

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

然后按上述方式使用

答案 1 :(得分:0)

有两种方法。 一个是使用收集

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
}

这里的问题是删除大括号({})之间的逗号,即“{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]; 
        }                        
    }    

}

希望这有帮助!