Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse CSV with double quote in some cases

Tags:

I have csv that comes with format:

a1, a2, a3, "a4,a5", a6

Only field with , will have quotes

Using Java, how to easily parse this? I try to avoid using open source CSV parser as company policy. Thanks.

like image 972
HP. Avatar asked Oct 17 '11 22:10

HP.


People also ask

How do I escape a double quote in a CSV file?

Yes. You can import double quotation marks using CSV files and import maps by escaping the double quotation marks. To escape the double quotation marks, enclose them within another double quotation mark.

Why are there double quotes in my CSV file?

ISSUE: A CSV file contains data that consists of a bunch of fields separated by a comma and optionally enclosed by double-quotes, hence the name Comma-Separated-Values or CSV. Due to the lack of an actual standard for CSV formatting, some programs may opt to use semi-colons instead of commas as separators.

How do I read a CSV file in a quote?

For read. csv(), the default quote parameter is quote="\"", which means that only double quotes will be used to delimit strings, not single quotes. Because two of your sample names had apostrophes (single quotes), the read. table() function tried to include everything between those two as a single string.


1 Answers

You could use Matcher.find with the following regular expression:

 \s*("[^"]*"|[^,]*)\s* 

Here's a more complete example:

String s = "a1, a2, a3, \"a4,a5\", a6"; Pattern pattern = Pattern.compile("\\s*(\"[^\"]*\"|[^,]*)\\s*"); Matcher matcher = pattern.matcher(s); while (matcher.find()) {     System.out.println(matcher.group(1)); } 

See it working online: ideone

like image 86
Mark Byers Avatar answered Oct 21 '22 14:10

Mark Byers