-1

I'm working on a code where i have a large csv file that is being parsed by java program. I'm using Apache Commons Csv parser to parse that file in java program.

Problem: Whenever there is some cell in csv file that has value like Yes, I am a developer the CSV parser treats Yes as one column and I am a developer as different coulmn (i.e. it reads & treats the cell as two different columns while parsing which should not be done as it's a value within same cell)

When editing the file i found that whenever a csv file cell contains a comma in its value then that's cell value is interpreted as in double quotes.

Any help here in escaping the comma which is in cell value will be very helpful?

Thanks in advance!

  • 2
    So, if the delimiter in the CSV is a `,` character, and each field does not also have double-quotes around fields that have an embedded comma, you don't have much chance. See the "Related" questions to the right of yours, and also a look at https://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/package-summary.html and https://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.Predefined.html – Mark Stewart Jan 07 '21 at 20:41
  • 1
    Can you [edit] your question to provide a [mre] (code and data)? Apache Commons CSV should have no problem parsing a CSV field containing a field such as `"Yes, I am a developer"` correctly. – andrewJames Jan 07 '21 at 22:16
  • 2
    Also, is there a reason for the `apache-poi` tag in your question? If the CSV data was created from an Excel spreadsheet, then you may need to handle [byte order marks](https://commons.apache.org/proper/commons-csv/user-guide.html#Example:_Parsing_an_Excel_CSV_File) as well. – andrewJames Jan 07 '21 at 22:17

1 Answers1

1

Enclose the field in quotes, e.g.

field1_value,field2_value,"field 3,value",field4, etc...

Check here: Link

  • But here, there is no possibility of modifying the csv file as it always has dynamic content. please suggest some way using apache commons csv parser if possible – Dhruv Kumar Sood Jan 07 '21 at 20:11
  • 3
    If you don't have access to the CSV file during creation, I'm not sure if there is any way to scape this kinds of comma. If yes, as I said you can put the cell in double quote or change the separator to something not exist in file like pipe | –  Jan 07 '21 at 20:16