0

I'm using Java CSVReader and CSVWriter from OpenCSV

    public static void main(String[] args) throws IOException {
        File inputFile=new File("e:\\temp\\5.csv");
        File outputFile=new File("e:\\temp\\5_out.csv");
        CSVReader reader=new CSVReader(new FileReader(inputFile));
        CSVWriter outputWriter = new CSVWriter(new FileWriter(outputFile),',', '"');
        //CSVWriter outputWriter = new CSVWriter(new FileWriter(outputFile),',', '"','\\');
        Listlines=reader.readAll();

        outputWriter.writeAll(lines);
        outputWriter.flush();
        outputWriter.close();
        reader.close();
    }

input1

"\"7 BURY NEW ROAD  MANCHESTER","","","","Prestwich"

output1

"""7 BURY NEW ROAD  MANCHESTER","","","","Prestwich"

I don't understand why the output is like that. Can someone explain the logic?

If I uncomment the lines where it's commented, basically tells the writer to use '\' to escape quotes and escape characters, it has the following results:

output2

"\"7 BURY NEW ROAD  MANCHESTER","","","","Prestwich"

I also don't understand why the output is like that.

1
  • 1
    That output looks reasonable. What did you expect the output to be like? CSV didn't really have a standard format until 2005 (and even that is just a recommendation, not a rule) and tends to vary a bit between implementations, but traditionally double quotes are escaped with "", not \", and RFC4180 specifies "". Commented Mar 21, 2014 at 5:00

1 Answer 1

0

I think it makes sense. CSVReader uses \ as escape character and it seems that it can't be changed. Once read, your input starts with the following structure:

DELIMITER - ESCAPED_QUOTATION_MARK

Your first CSVWriter's escape character is ", so the first output starts with

"""

Your second CSVWriter escapes with \, so

"\"

the output is.

(is the disappearing MANCHESTER also a part of the issue or just a typo?)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.