xlson / groovycsv

A simple CSV parsing library for groovy
http://xlson.com/groovycsv/
Other
139 stars 32 forks source link

Cannot parse double quote if is at the end of the line #42

Closed karaabova closed 6 years ago

karaabova commented 6 years ago

Hello,

I faced an issue, when trying to parse a file with a line that ends with quotes. The parser doesn't read the ending quote, but if I add a char or space after the ending quote (line 1, from the file, ends with space), it is fine.

Let me share an example, a file with the following content:

id;test 1;project = "Test 1" 2;project = "Test 2"

Code:

def data = parseCsv(new FileReader(new File("tmp.csv")), separator: ';')
 for(line in data) {
        println "$line"
}

Output:

id: 1, test: project = "Test 1" 
id: 2, test: project = "Test 2
xlson commented 6 years ago

Hi @karaabova,

Thanks for taking the time to describe your issue in detail, that makes it a lot easier for me to help.

The reason you're having problems is that " is the default quote char and is intended to surround everything between a separator and either another separator or the beginning/end of a line. You can simply change quote char to some other character that you aren't using.

This would work:

def data = parseCsv(new FileReader(new File("tmp.csv")), separator: ';', quoteChar: ':')
 for(line in data) {
        println "$line"
}

If you're in control of how the csv is being generated another option might be to escape the quotes ( \" ).

Hope this helps. Cheers!