首页 > 解决方案 > 如何使用opencsv正确处理带引号的字符串中的逗号?

问题描述

我正在尝试读取包含带引号和不带引号的字符串的 csv 文件。

我已经尝试了多种方法,但到目前为止没有任何效果。

当前测试数据:

"field1 (with use of , we lose the other part)",some description
field2,"Dear %s, some text"

获取映射 bean 的第一个字段

预期结果:

"field1 (with use of , we lose the other part)"
field2

当前结果:

"field1 (with use of 
field2

这是代码:

public class CsvToBeanReaderTest {

    @Test
    void shouldIncludeDoubleQuotes() {
        String testData =
                "\"field1 (with use of , we lose the other part)\",some description\n"
                        +
                        "field2,\"Dear %s, some text\"";

        RFC4180ParserBuilder rfc4180ParserBuilder = new RFC4180ParserBuilder();
        rfc4180ParserBuilder.withQuoteChar(ICSVWriter.NO_QUOTE_CHARACTER);
        ICSVParser rfc4180Parser = rfc4180ParserBuilder.build();
        CSVReaderBuilder builder = new CSVReaderBuilder(new StringReader(testData));

        CSVReader reader = builder
                .withCSVParser(rfc4180Parser)
                .build();

        List<TestClass> result = new CsvToBeanBuilder<TestClass>(reader)
                .withType(TestClass.class)
                .withEscapeChar('\"')
                .build()
                .parse();

        result.forEach(testClass -> System.out.println(testClass.getField1()));
    }

    private List<TestClass> readTestData(String testData) {
        return new CsvToBeanBuilder<TestClass>(new StringReader(testData))
                .withType(TestClass.class)
                .withSeparator(',')
                .withSkipLines(0)
                .withIgnoreEmptyLine(true)
                .build()
                .parse();
    }

    public static final class TestClass {
        @CsvBindByPosition(position = 0)
        private String field1;

        @CsvBindByPosition(position = 1)
        private String description;

        public String toCsvFormat() {
            return String.join(",",
                    field1,
                    description);
        }

        public String getField1() {
            return field1;
        }
    }
}

我发现如果我评论或删除rfc4180ParserBuilder.withQuoteChar(ICSVWriter.NO_QUOTE_CHARACTER);字符串将被正确解析,但我会丢失不应丢失的引号字符。有什么建议可以做吗?(我不希望打开其他 csv 库)

标签: javaparsingjavabeansopencsv

解决方案


推荐阅读