【问题标题】:Ignore newline in record value while using Apache Common CSV使用 Apache Common CSV 时忽略记录值中的换行符
【发布时间】:2018-01-29 12:14:02
【问题描述】:

我正在使用 apache common csv 解析一个 csv,但我得到的记录更多,然后它存在于 csv 中,这是因为在我的 csv 中我有一些包含记录分隔符 (\n) 的值用双引号括起来。

https://commons.apache.org/proper/commons-csv/

例子:

test, "test
test 2",test 3, test 4 

我期待 o/p 为

Record 1: test
Record 2: 
test
test 2
Record 3: test 3
Record 4: test 4

但我越来越喜欢这个了

Record 1: test
Record 2: test
Record 3: test 2
Record 4: test 3
Record 5: test 4

这是我目前用于解析的代码。

CSVParser 解析器 = CSVParser.parse(reader,CSVFormat.RFC4180.withFirstRecordAsHeader() .withQuote(null));

只是补充一下,我之前遇到了在记录字段值中使用字段分隔符的问题,该问题已通过 .withQuote(null) 解决。

关于我们如何解决这个问题的任何线索?

【问题讨论】:

  • 显示您的代码。
  • 我们总是乐于帮助和支持新的编码员,但您需要先帮助自己。 :-) After doing more research 如果您有问题,请发布您尝试过的内容,并清楚地解释什么不起作用并提供Minimal, Complete, and Verifiable example。阅读How to Ask 一个好问题。请务必take the tour 并阅读this
  • 这是我目前用于解析的代码。 CSVParser 解析器 = CSVParser.parse(reader, CSVFormat.RFC4180.withFirstRecordAsHeader() .withQuote(null));只是补充一下,我之前遇到了在记录字段值中使用字段分隔符的问题,该问题已通过 .withQuote(null) 解决。

标签: csv parsing


【解决方案1】:

当您将引号字符设置为 null 时,我看不出这是如何工作的。 RFC4180 将引号字符设置为双引号,这是您想要的。

【讨论】:

  • 如果我没有看到 withQuote(null) 如下所示,那么我会收到此运行时异常“(第 12494 行)封装令牌和分隔符之间的无效字符”。看起来这个问题stackoverflow.com/questions/26729799/… 所以我添加了 withQuote(null) 来解决这个问题。 CSVParser 解析器 = CSVParser.parse(reader, CSVFormat.RFC4180.withFirstRecordAsHeader());还有其他解决方法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多