How to determine the delimiter in CSV file

前端 未结 5 1273
旧时难觅i
旧时难觅i 2020-12-16 03:26

I have a scenario at which i have to parse CSV files from different sources, the parsing code is very simple and straightforward.

        String csvFile = \         


        
5条回答
  •  余生分开走
    2020-12-16 04:07

    univocity-parsers supports automatic detection of the delimiter (also line endings and quotes). Just use it instead of fighting with your code:

    CsvParserSettings settings = new CsvParserSettings();
    settings.detectFormatAutomatically();
    
    CsvParser parser = new CsvParser(settings);
    List rows = parser.parseAll(new File("/path/to/your.csv"));
    
    // if you want to see what it detected
    CsvFormat format = parser.getDetectedFormat();
    

    Disclaimer: I'm the author of this library and I made sure all sorts of corner cases are covered. It's open source and free (Apache 2.0 license)

    Hope this helps.

提交回复
热议问题