Reputation: 51
I am trying to parse a CSV with Quotes using Univocity CSV, I find that after parsing the close quotes are missing from some values.
CSV:
ACCT,NAME,AGE,ADDRESS
700,GINI,23,"AB,ECITY-1"
800,HANNAH,30,"AB,ECITY-1"
900,IAN,40,"XYZ,ECITY-1"
1900,LYDIA,40,"XYZ,ECITY-1"
Output:
[GINI, "AB,ECITY-1]
[HANNAH, "AB,ECITY-1]
[IAN, "XYZ,ECITY-1]
[LYDIA, "XYZ,ECITY-1]
CsvParserSettings settings = new CsvParserSettings();
settings.getFormat().setLineSeparator("\n");
settings.setKeepQuotes(true);
settings.setQuoteDetectionEnabled(false);
settings.trimValues(true);
settings.excludeFields(excludeHeaders.split("ACCT,AGE"));
CsvParser baseFileParser = new CsvParser(settings);
String[] baseRow;
baseFileParser.beginParsing(baseFile);
while((baseRow = baseFileParser.parseNext())!= null){
System.out.println(Arrays.toString(baseRow));
}
What am I missing. Why is the close quote missing in the output?
Upvotes: 1
Views: 142
Reputation: 6289
Are you using version 2.5.8? If not then this looks related to a bug I fixed recently. Just update and it will work.
A little explanation:
In your case the bug will manifest itself if you set settings.getFormat().setLineSeparator("\n");
but the input file has \r\n
as the separator. The \r
will he handled by the parser as a whitespace and trigger the bug.
That's why removing settings.getFormat().setLineSeparator("\n");
worked for you - if you are on Windows, the default line separator used by the parser will be \r\n
. This will avoid the bug as there is no trailing whitespace before the line ending.
Upvotes: 0
Reputation: 51
Found the answer myslef.. Had to remove the below line
settings.getFormat().setLineSeparator("\n");
Code works fine if the above line is removed
Upvotes: 1