I am trying to parse csv using java and have the following problem: the second column is a String (which may also contain a comma), enclosed in double quotes, except that the string itself contains a double quote, then the whole string is attached with one quote. eg.
The lines may look like this:
someStuff,"hello", someStuff
someStuff,"hello, SO", someStuff
someStuff,'say "hello, world"', someStuff
someStuff,'say "hello, world', someStuff
someStuff are placeholders for other elements, which may also include quotes in the same style.
I am looking for a general way to separate strings with UNLESS commas, enclosed in single double quotes OR, to get the second column as String. With the second column, I mean the fields:
- Hello
- hi SO
- say hello world
- say hello world
OpenCSV, , :
public class CSVDemo {
public static void main(String[] args) throws IOException {
CSVDemo demo = new CSVDemo();
demo.process("input.csv");
}
public void process(String fileName) throws IOException {
String file = this.getClass().getClassLoader().getResource(fileName)
.getFile();
CSVReader reader = new CSVReader(new FileReader(file));
String[] nextLine;
while ((nextLine = reader.readNext()) != null) {
System.out.println(nextLine[0] + " | " + nextLine[1] + " | "
+ nextLine[2]);
}
}
}
opencsv , , :
someStuff | hello | someStuff
someStuff | hello, SO | someStuff
someStuff | 'say "hello, world"' | someStuff
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1