Menu

#79 how to change value delimiter?

v1.0 (example)
closed
None
5
2019-12-02
2019-12-02
Apolo
No

Is there a way to change value delimiter instead of the default double quotes " ?

I am wondering what will happen if I have a column which contains one single double quote such as ...

text"text2

Discussion

  • Andrew Rucker Jones

    • status: open --> closed
    • assigned_to: Andrew Rucker Jones
     
  • Andrew Rucker Jones

    withSeparator() in whatever builder you are using directly (CsvToBeanBuilder, most likely).

     
  • Scott Conway

    Scott Conway - 2019-12-02

    Look at the CSVParserTest if you have downloaded the code.

    Your request made me realize we commented all this very well in the javadocs (Look at the javadocs for CSVParserBuilder or RFC4180ParserBuilder) but the example we have on the sourceforge page (http://opencsv.sourceforge.net/) could use some updating. Will try and look into tat in the next release.

    but here is one of the unit tests we have that will give you an example.

    /**
     * Test issue 2263439 where an escaped quote was causing the parse to fail.
     * Special thanks to Chris Morris for fixing this (id 1979054)
     *
     * @throws IOException But not really
     */
    @Test
    public void testIssue2263439() throws IOException {
        csvParser = new CSVParserBuilder()
                .withSeparator(',')
                .withQuoteChar('\'')
                .build();
    
        String[] nextLine = csvParser.parseLine("865,0,'AmeriKKKa\\'s_Most_Wanted','',294,0,0,0.734338696798625,'20081002052147',242429208,18448");
    
        assertEquals(11, nextLine.length);
    
        assertEquals("865", nextLine[0]);
        assertEquals("0", nextLine[1]);
        assertEquals("AmeriKKKa's_Most_Wanted", nextLine[2]);
        assertEquals("", nextLine[3]);
        assertEquals("18448", nextLine[10]);
    
    }
    
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.