opencsv in java ignores backslash in a field value

ahaneo picture ahaneo · May 15, 2011 · Viewed 8.5k times · Source

I am reading a csv file using opencsv.

I am ignoring the first line of; the csv file is tab separated with some values enclosed in double quotes.

The problem occurs when I read the values of a column that has the '\' character, this is stripped out of the value.

reader = new CSVReader(new FileReader(exchFileObj),'\t','"',1);

For example in original file:

address = 12\91buenosaires   

It becomes as:

address = 1291buenosiares

In the string array that csvreader generates. How do I modify it to be able to read the '\' character also?

Answer

JMM picture JMM · Dec 19, 2011

I had the same problem and couldn't find another character I could guarantee wouldn't show up in my csv file. According to a post on sourceforge though, you can use the explicit constructor with a '\0' to indicate that you don't want any escape character.

http://sourceforge.net/tracker/?func=detail&aid=2983890&group_id=148905&atid=773542

CSVParser parser = new CSVParser(CSVParser.DEFAULT_SEPARATOR, CSVParser.DEFAULT_QUOTE_CHARACTER, '\0', CSVParser.DEFAULT_STRICT_QUOTES);

I did a bit of cursory testing, and this seems to work just fine, at least backslashes certainly make it through.