0

Possible Duplicate:
Java: splitting a comma-separated string but ignoring commas in quotes

Is there a way to handle commas inside values in a csv file?

For example if there's a company name, Some Company, Inc.. The usual parsing will interpret the comma between company and Inc as separator between values, and this throws everything off.

I have no control over the csv file -- I need to work with it the way it is. Is there a solution in Java?

EDIT: the solution can be found here

Community
  • 1
  • 1
Eddy
  • 3,273
  • 13
  • 56
  • 84
  • 3
    Is Some Company, Inc enclosed in quotes: `abc,"Some Company, Inc",def,ghi`? – assylias Feb 05 '13 at 12:24
  • Please post some part of your csv file that allows us to reproduce the problem. – Tim Pietzcker Feb 05 '13 at 12:27
  • Yes, assylias, it is in quotes. Exactly like you snippet above. – Eddy Feb 05 '13 at 13:20
  • OK, the solution can be found [here](http://stackoverflow.com/questions/1757065/java-splitting-a-comma-separated-string-but-ignoring-commas-in-quotes) Thank you assylias for putting me in the right direction. – Eddy Feb 05 '13 at 13:39
  • FYI, read the standard spec for CSV: [RFC 4180](https://tools.ietf.org/html/rfc4180). – Basil Bourque Jul 04 '19 at 01:22

3 Answers3

0

If the CSV file was generated with quoting you should use them. Otherwise you'll have to create a tool that automatically escapes the comma based on some recognizable pattern before processing the file.

jspboix
  • 706
  • 1
  • 4
  • 8
0

Use OpenCSV, that is a widely used library to handle csv format.

Jayan
  • 17,353
  • 13
  • 86
  • 137
0

I usually use csv files separated with semi-colon instead of coma. Basically a csv file is a text file with separators. You can use many separators. For example, you can test it by creating a text file, add few items separated by semi-colon and then change the file extension to csv.

Here's a link giving more information

iDev
  • 496
  • 5
  • 24
  • He says he doesn't have any control over the CSV file. – Tim Pietzcker Feb 05 '13 at 12:39
  • I've a semicolon sperated csv file. do you know how to include values with comma in such a case ? (double-quotes not working) – Alisha Raju Oct 09 '18 at 10:35
  • @AlishaRaju did you take a look at the link provided in the question? I'm copying it [here](https://stackoverflow.com/questions/1757065/java-splitting-a-comma-separated-string-but-ignoring-commas-in-quotes). Let me know if that helps. – iDev Oct 09 '18 at 12:33
  • Hi @iDev , In the document you shared, you had ', ' seperated values. In my scenerio, the values are *semicolon* seperated. say ::: abc;dcf;*efg,ghm*;hlkm – Alisha Raju Oct 24 '18 at 09:47
  • true! @AlishaRaju – iDev Oct 25 '18 at 17:19