如何以编程方式猜测CSV文件是否以逗号或分号分隔 [英] How to programmatically guess whether a CSV file is comma or semicolon delimited

查看:383
本文介绍了如何以编程方式猜测CSV文件是否以逗号或分号分隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在大多数情况下,CSV文件是带有逗号分隔的记录的文本文件。但是,有时这些文件将以分号分隔。 (如果区域设置的小数分隔符设置为逗号,Excel将在保存CSV时使用分号分隔符 - 这在欧洲很常见。参考: http://en.wikipedia.org/wiki/Comma-separated_values#Application_support

In most cases, CSV files are text files with records delimited by commas. However, sometimes these files will come semicolon delimited. (Excel will use semicolon delimiters when saving CSVs if the regional settings has the decimal separator set as the comma -- this is common in Europe. Ref: http://en.wikipedia.org/wiki/Comma-separated_values#Application_support)

我的问题是,让程序猜测是否使用逗号或分号分隔的最好方法是什么?

My question is, what is the best way to have a program guess whether to have it comma or semicolon separated?

例如像1,1; 1,1的线可能是不明确的。它可以用逗号分隔为:
1
1; 1(字符串)
1

e.g. a line like 1,1;1,1 may be ambiguous. It could be interpreted comma delimited as: 1 1;1 (a string) 1

或分号分隔为
1,1
1,1

or semicolon delimited as 1,1 1,1

我最好的猜测是到目前为止,尝试使用和解析文件。分隔符,然后选择具有与第一行(通常为标题行)相同长度的最多行的解析。如果两者具有相同的行数,请选择具有更多列的行。

My best guess so far is to try parsing the file both with , and ; delimiters, then choose the parse that has the most rows of the same length as the first row (usually a header row). If both have the same number of rows, choose the one with more columns. The main disadvantage of this is the extra overhead.

推荐答案

您可以读取第一行

FileReader fileReader = new FileReader(filePath);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    String s = bufferedReader.readLine();
    String substring = s.substring(s.indexOf(firstColumnName) + 3, s.indexOf(firstColumnName) + 4);
    bufferedReader.close();
    fileReader.close();
    substring.charAt(0);

然后您捕获此值


substring.charAt(0)

substring.charAt(0)

取决于CSV是逗号还是分号可以使用最后一个值

depending on whether the CSV is comma or semicolon can use the last value

这篇关于如何以编程方式猜测CSV文件是否以逗号或分号分隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆