如何确定CSV文件中的分隔符

如何确定CSV文件中的分隔符

本文介绍了如何确定CSV文件中的分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我必须解析来自不同来源的CSV文件,解析代码非常简单明了.

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 = "/Users/csv/country.csv";
        String line = "";
        String cvsSplitBy = ",";
        try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {
            while ((line = br.readLine()) != null) {
                // use comma as separator
                String[] country = line.split(cvsSplitBy);
                System.out.println("Country [code= " + country[4] + " , name=" + country[5] + "]");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

我的问题来自CSV分隔符,我有许多不同的格式,有时是,,有时是;

my problem come from the CSV delimiter character, i have many different formats, some time it is a , sometimes it is a ;

在解析文件之前是否有任何方法可以确定定界符

is there is any way to determine the delimiter character before parsing the file

推荐答案

univocity-parsers 支持自动检测定界符(也包括行尾和引号).只需使用它,而不用与您的代码作斗争:

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<String[]> rows = parser.parseAll(new File("/path/to/your.csv"));

// if you want to see what it detected
CsvFormat format = parser.getDetectedFormat();

免责声明:我是该库的作者,并且确保确保涵盖了各种极端情况.它是开源且免费的(Apache 2.0许可证)

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)

希望这会有所帮助.

这篇关于如何确定CSV文件中的分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 03:11