在Apache Commons CSV / OpenCSV中忽略引号括起字段中的分隔符?

时间:2016-11-14 21:57:26

标签: java csv noclassdeffounderror apache-commons apache-commons-csv

我必须解析一个csv文件,该文件的字段类似于以下内容:

("FOO, BAR BAZ", 42)

并产生两个字段:

FOO, BAR BAZ  
42

我不确定如何使用Apache Commons CSV或OpenCSV简洁地执行此操作,因此我正在寻找一些指导。可能只是因为我没有完全理解org.apache.commons.csv.CSVFormat属性“quoteChar”which is touched on in the documentation,但从未明确解释过任何我能找到的地方。如果是这样,如果你能指出我对该功能的更好记录,那将非常有用。

这是一个简短的例子,展示了我的问题以及我尝试过的内容和结果:

        String test = "(\"FOO, BAR BAZ\", 42)";
        int numTries = 5;
        CSVParser[] tries = new CSVParser[numTries];
        tries[0] = CSVParser.parse(line, CSVFormat.DEFAULT.withRecordSeparator("\n"));//BAR BAZ"
        tries[1] = CSVParser.parse(line, CSVFormat.DEFAULT.withQuote('"'));//BAR BAZ"
        tries[2] = CSVParser.parse(line, CSVFormat.DEFAULT.withQuote(null));//BAR BAZ"
        tries[3] = CSVParser.parse(line, CSVFormat.DEFAULT.withQuote('"').withQuoteMode(QuoteMode.NON_NUMERIC));//BAR BAZ"
        tries[4] = CSVParser.parse(line, CSVFormat.DEFAULT.withRecordSeparator(")\n("));//BAR BAZ"

        for(int i = 0; i < numTries; i++){
            CSVRecord record = tries[i].getRecords().get(0);
            System.out.println(record.get(1));//.equals("42"));
        }  

请注意,如果从输入中排除括号,它可以正常工作。

3 个答案:

答案 0 :(得分:0)

您可以使用OpenCSV的{​​{1}}来读取数据并获取数据元素,如下所示:

CSVReader

答案 1 :(得分:0)

对我来说,commons-csv的默认格式对于格式正确的CSV消息是正确的:

    Reader in = new StringReader("\"FOO, BAR BAZ\", 42");
    Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(in);
    for (CSVRecord record : records) {
        for(int i = 0;i < record.size();i++) {
            System.out.println("At " + i + ": " + record.get(i));
        }
    }

导致:

At 0: FOO, BAR BAZ
At 1:  42

对于特殊格式的线条,您可能需要更多处理顶部删除这些括号:

    BufferedReader lineReader = new BufferedReader(
            new StringReader("(\"FOO, BAR BAZ\", 42)\n(\"FOO, BAR FOO\", 44)"));

    while(true) {
        String line = lineReader.readLine();
        if (line == null) {
            break;
        }

        String adjustedLine = line.substring(1, line.length() - 1);
        records = CSVFormat.DEFAULT.parse(new StringReader(adjustedLine));
        for (CSVRecord record : records) {
            for (int i = 0; i < record.size(); i++) {
                System.out.println("At " + i + ": " + record.get(i));
            }
        }
    }

答案 2 :(得分:0)

您可以使用opencsv实现此目的,如下所示:

import com.opencsv.CSVReader;
import java.io.FileReader;
import java.io.IOException;

public class NewClass1 {
    public static void main(String[] args) throws IOException {
        String fileName = "C:\\yourFile.csv";
        String [] nextLine;
        // use the three arg constructor to tell the reader which delimiter you have in your file(2nd arg : here ',')                                                          
        // you can change this to '\t' if you have tab separeted file or ';' or ':' ... whatever your delimiter is
        // (3rd arg) '"' if your fields are double quoted or '\'' if single quoted or no 3rd arg if the fields are not quoted
        CSVReader reader = new CSVReader(new FileReader(fileName), ',' ,'"');
        // nextLine[] is an array of values from the line
        // each line represented by String[], and each field as an element of the array
        while ((nextLine = reader.readNext()) != null) {        
            System.out.println("nextLine[0]: " +nextLine[0]);
            System.out.println("nextLine[1]: " +nextLine[1]);
        }
    }
}