I have a simular issue to http://stackoverflow.com/questions/32644150/opencsv-reading-file-with-escaped-separator. I need to escape the delimiter but I am not using quotechar.
I am not sure if this was done deliberately but it looks like the fix is very simple
public class CSVWriter
...
/
* Appends the character to the StringBuilder adding the escape character if
* needed.
*
* @param sb
* - StringBuffer holding the processed character.
* @param nextChar
* - character to process
*/
private void processCharacter(StringBuilder sb, char nextChar) {
if (escapechar != NO_ESCAPE_CHARACTER && (nextChar == quotechar** || nextChar == escapechar || nextChar == separator)) {
sb.append(escapechar).append(nextChar);
} else {
sb.append(nextChar);
}
}
Hello Haim - sorry it has been so long to respond but work and family have kept me busy.
Please make sure you are using the 3.6 openCSV. If you are then please check the unit test I created to try and recreate your issue. This test is passing when according to what you are saying it should fail so if I am misunderstanding the problem please let me know.
@Test
public void issue123SeparatorEscapedWhenQuoteIsNoQuoteChar() {
String[] header = {"Foo", "Bar", "baz"};
String[] value = {"v1", "v2" + CSVWriter.DEFAULT_ESCAPE_CHARACTER +"v2a", "v3"};
}
I cloned Release-3.6 branch and run the flowing modification of your tests.
For example in the secound test I expected the data in the secound cell v2\v2a,v2b
to be v2\v2a\,v2b
org.junit.ComparisonFailure: expected:<...o,Bar,baz
v1,v2""v2a["],v2b,v3
org.junit.ComparisonFailure: expected:<...o,Bar,baz
v1,v2\v2a[],v2b,v3
Last edit: Haim 2015-12-24
Thanks for the sample test! That allowed me to pin down the issue and come up with a fix.
I had to expand on your suggestion because if we do have a quote character defined we do not need or want to escape the separator character. So I ended up doing this:
private void processCharacter(StringBuilder sb, char nextChar) {
if (escapechar != NO_ESCAPE_CHARACTER && checkCharactersToEscape(nextChar)) {
sb.append(escapechar).append(nextChar);
} else {
sb.append(nextChar);
}
}
private boolean checkCharactersToEscape(char nextChar) {
return quotechar == NO_QUOTE_CHARACTER ?
(nextChar == quotechar || nextChar == escapechar || nextChar == separator)
: (nextChar == quotechar || nextChar == escapechar);
}
I have merged this into trunk and will be in the 3.7 release.