用带引号的字段内的双引号将OpenCSV解析为CSV [英] Parse CSV with OpenCSV with double quotes inside a quoted field

查看:642
本文介绍了用带引号的字段内的双引号将OpenCSV解析为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用OpenCSV解析CSV文件.其中一列以YAML序列化格式存储数据,并被引用,因为其中可以包含逗号.它的内部也带有引号,因此可以通过放置两个引号来对其进行转义.我可以在Ruby中轻松解析此文件,但是使用OpenCSV不能完全解析它.这是UTF-8编码的文件.

I am trying to parse a CSV file using OpenCSV. One of the columns stores the data in YAML serialized format and is quoted because it can have comma inside it. It also has quotes inside it, so it is escaped by putting two quotes. I am able to parse this file easily in Ruby, but with OpenCSV I am not able to parse it fully. It is a UTF-8 encoded file.

这是我的Java代码段,正在尝试读取文件

Here is my Java snippet which is trying to read the file

CSVReader reader = new CSVReader(new InputStreamReader(new FileInputStream(csvFilePath), "UTF-8"), ',', '\"', '\\');

这里是此文件的2行.由于我猜测转义的双引号,第一行未正确解析,并在""[Fair Trade Certified]""处被拆分.

Here are 2 lines from this file. First line is not being parsed properly and is getting split at ""[Fair Trade Certified]"" because of escaped double quotes I guess.

1061658767,update,1196916,Product,28613099,Product::Source,"---
product_attributes:
-
- :name: Ornaments
  :brand_id: 49120
  :size: each
  :alcoholic: false
  :details: ""[Fair Trade Certified]""
  :gluten_free: false
  :kosher: false
  :low_fat: false
  :organic: false
  :sugar_free: false
  :fat_free: false
  :vegan: false
  :vegetarian: false
",,2015-11-01 00:06:19.796944,,,,,,
1061658768,create,,,28613100,Product::Source,"---
product_id:
retailer_id:
store_id:
source_id: 333790
locale: en_us
source_type: Product::PrehistoricProductDatum
priority: 1
is_definition:
product_attributes:
",,2015-11-01 00:06:19.927948,,,,,,

推荐答案

解决方案是使用保罗.我曾经使用过OpenCSV的CSVReader,但它无法正常工作,或者可能无法正常工作.

The solution was to use a RFC4180 compatible CSV parser, as suggested by Paul. I had used CSVReader from OpenCSV which didn't work or maybe I couldn't get it to work properly.

我使用了RFC4180 CSV解析器 FastCSV ,它可以无缝运行.

I used FastCSV, a RFC4180 CSV parser, and it worked seamlessly.

File file = new File(csvFilePath);
CsvReader csvReader = new CsvReader();
CsvContainer csv = csvReader.read(file, StandardCharsets.UTF_8);
for (CsvRow row : csv.getRows()) {
    System.out.println(row.getFieldCount());  
}

这篇关于用带引号的字段内的双引号将OpenCSV解析为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆