Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape comma and double quote at same time for CSV file?

I am writing a Java app to export data from Oracle to csv file

Unfortunately the content of data may quite tricky. Still comma is the deliminator, but some data on a row could be like this:

| ID    |   FN    |   LN   |  AGE   |  COMMENT                   | |----------------------------------------------------------------| | 123   |  John   |  Smith |   39   | I said "Hey, I am 5'10"."  | |----------------------------------------------------------------| 

so this is one of the string on the comment column:

I said "Hey, I am 5'10"."

No kidding, I need to show above comment without compromise in excel or open office from a CSV file generated by Java, and of course cannot mess up other regular escaping situation(i.e. regular double quotes, and regular comma within a tuple). I know regular expression is powerful but how can we achieve the goal with such complicated situation?

like image 601
Dreamer Avatar asked May 04 '12 15:05

Dreamer


People also ask

How do you handle double quotes and commas in a CSV file?

Since CSV files use the comma character "," to separate columns, values that contain commas must be handled as a special case. These fields are wrapped within double quotation marks. The first double quote signifies the beginning of the column data, and the last double quote marks the end.

How do you escape a comma when writing a CSV file?

Re: Handling 'comma' in the data while writing to a CSV. So for data fields that contain a comma, you should just be able to wrap them in a double quote. Fields containing line breaks (CRLF), double quotes, and commas should be enclosed in double-quotes.


1 Answers

There are several libraries. Here are two examples:


❐ Apache Commons Lang

Apache Commons Lang includes a special class to escape or unescape strings (CSV, EcmaScript, HTML, Java, Json, XML): org.apache.commons.lang3.StringEscapeUtils.

  • Escape to CSV

    String escaped = StringEscapeUtils     .escapeCsv("I said \"Hey, I am 5'10\".\""); // I said "Hey, I am 5'10"."  System.out.println(escaped); // "I said ""Hey, I am 5'10"".""" 
  • Unescape from CSV

    String unescaped = StringEscapeUtils     .unescapeCsv("\"I said \"\"Hey, I am 5'10\"\".\"\"\""); // "I said ""Hey, I am 5'10""."""  System.out.println(unescaped); // I said "Hey, I am 5'10"." 

* You can download it from here.


❐ OpenCSV

If you use OpenCSV, you will not need to worry about escape or unescape, only for write or read the content.

  • Writing file:

    FileOutputStream fos = new FileOutputStream("awesomefile.csv");  OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); CSVWriter writer = new CSVWriter(osw); ... String[] row = {     "123",      "John",      "Smith",      "39",      "I said \"Hey, I am 5'10\".\"" }; writer.writeNext(row); ... writer.close(); osw.close(); os.close(); 
  • Reading file:

    FileInputStream fis = new FileInputStream("awesomefile.csv");  InputStreamReader isr = new InputStreamReader(fis, "UTF-8"); CSVReader reader = new CSVReader(isr);  for (String[] row; (row = reader.readNext()) != null;) {     System.out.println(Arrays.toString(row)); }  reader.close(); isr.close(); fis.close(); 

* You can download it from here.

like image 128
Paul Vargas Avatar answered Sep 19 '22 13:09

Paul Vargas