【问题标题】:Apache commons CSV: quoted input doesn't workApache commons CSV:引用的输入不起作用
【发布时间】:2016-11-20 19:49:57
【问题描述】:
import java.io.IOException;
import java.io.StringReader;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;

我尝试使用 Apache CSV 解析器解析一个简单的 csv 文件。只要我不使用引号,它就可以正常工作。当我尝试在输入中添加引号时

"a";42

它给了我错误:

invalid char between encapsulated token and delimiter

这是一个简单而完整的代码:

public class Test {

    public static void main(String[] args) throws IOException {
        String DATA = "\"a\";12";
        CSVParser csvParser =    
        CSVFormat.EXCEL
            .withIgnoreEmptyLines()
            .withIgnoreHeaderCase()
            .withRecordSeparator('\n').withQuote('"')
            .withEscape('\\').withRecordSeparator(';').withTrim()
            .parse(new StringReader(DATA));
    }

}

我根本无法找出我在代码中遗漏了什么。

【问题讨论】:

    标签: java apache-commons-csv


    【解决方案1】:

    这个问题太琐碎了,我错过了。

    我使用withRecordSeparator 而不是withDelimiter 来设置字段分隔符。

    这符合我的预期:

    public class Test {
    
        public static void main(String[] args) throws IOException {
            String DATA = "\"a\";12";
            CSVParser csvParser =    
            CSVFormat.EXCEL
                .withIgnoreEmptyLines()
                .withIgnoreHeaderCase()
                .withRecordSeparator('\n').withQuote('"')
                .withEscape('\\').withDelimeter(';').withTrim()
                .parse(new StringReader(DATA));
        }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-26
      • 2017-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多