0

Is it possible to read a csv file where the values are separated by a string rather than a single character? Earlier the csv file had a comma separator, but now it is being created with a string separator "###". I don't have any control over the creation of the csv file.

I'm using the Opencsv library http://sourceforge.net/projects/opencsv/ and it does not seem to have support for string separator only characters.

Is there a way to solve the above? Are there any good and similar alternatives to the Opencsv library?

aldrin
  • 4,482
  • 1
  • 33
  • 50

1 Answers1

-3

The docs give you an example of how to do this. See http://opencsv.sourceforge.net/#custom-sepators.

Can I use my own separators and quote characters?
Yes. There are constructors that cater for supplying your own separator and quote characters. Say you're using a tab for your separator, you can do something like this:

  CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t');

And if you single quoted your escaped characters rather than double quote them, you can use the three arg constructor:

  CSVReader reader = new CSVReader(new FileReader("yourfile.csv"), '\t', '\'');
Qwerky
  • 18,217
  • 6
  • 44
  • 80
  • 1
    I've read that and they mention only single characters, not strings of characters – aldrin Aug 02 '11 at 10:47
  • @aldrin Apparently there is no open source Java library that can handle multi-character CSV separators, as per my recent relevant question (http://stackoverflow.com/questions/8653797/java-csv-parser-with-string-separator-multi-character). If, however, in the mean time you have found one, please contribute there. – PNS Dec 28 '11 at 10:41
  • @PNS no i didn't find an alternative. I tried to modify OpenCSV to process multi-char separators, but it wasn't trivial. So I eventually replaced the multichars with a single char as a preprocessing step and continued. – aldrin Jan 03 '12 at 06:19