简体   繁体   English

了解CSV文件的分隔符

[英]Knowing delimiters for CSV file

This may be a simple question but I have not been able to find a satisfactory answer. 这可能是一个简单的问题,但我找不到令人满意的答案。 I am writing a class in Java that needs to take in a .csv file filled with doubles in three columns. 我正在用Java写一个类,该类需要接收一个.csv文件,该文件在三列中均包含双打。 Obviously a .csv file uses commas as the delimiters, but when I try setting them with my scanner, the scanner finds nothing. 显然.csv文件使用逗号作为分隔符,但是当我尝试使用扫描仪设置它们时,扫描仪什么也找不到。 Any advice? 有什么建议吗?

Scanner s = null;
try {
  s = new Scanner(source);
  //s.useDelimiter("[\\s,\r\n]+"); //This one works if I am using a .txt file
  //s.useDelimiter(", \n"); // This is what I thought would work for a .csv file
  ...
} catch (FileNotFoundException e) {
  System.err.format("FileNotFoundException: %s%s", e);
} catch (IOException e) {
  System.err.format("IOException: %s%n", e);
}

A sample input would be: 输入示例如下:

12.3 11.2 27.0 12.3 11.2 27.0

0.5 97.1 18.3 0.5 97.1 18.3

etc. 等等

Thank you for your time! 感谢您的时间!

EDIT: fixed! 编辑:固定! Found the correct delimiters and realized I was using hasNextInt() instead of hasNextDouble(). 找到正确的定界符,并意识到我正在使用hasNextInt()而不是hasNextDouble()。 /facepalm / facepalm

Consider the following: 考虑以下:

first,second,"the third",fourth,"the,fifth"

Should only be five - the last comma is in a quote block, which should not get split. 应该只有五个-最后一个逗号在引号中,不要将其分开。

Don't reinvent the wheel. 不要重新发明轮子。 There are open source libraries to handle this behavior. 有开源库可以处理此行为。

A quick google search yielded http://opencsv.sourceforge.net/ and I'm sure there's others. 快速的Google搜索产生了http://opencsv.sourceforge.net/ ,我敢肯定还有其他人。

If you are trying to read each individual item, try: 如果您要阅读每个项目,请尝试:

s.useDelimiter(",");

Then s.next() would return an item from the CSV. 然后s.next()将从CSV返回一个项目。

Why have you got a \\n in your CSV delimiter? 为什么您的CSV分隔符中有\\ n? Java doesn't have a difference between CSV and TXT files, if they have the same content. 如果CSV和TXT文件具有相同的内容,则Java没有区别。

I would think you would want 我想你会想要

s.useDelimiter(",");

or 要么

s.useDelimiter("[\\s]+,[\\s\r\n]*");

There are several methods to workaround: 有几种解决方法:

Method 1: use conditional statements ( if-else / switch ) in file extension. 方法1:在文件扩展名中使用条件语句(if-else / switch)。

if(ext == 'csv') {
  s.useDelimiter(", \n");
} else if(ext == 'txt') {
  s.useDelimiter("[\\s,\r\n]+");
}

Method 2: as other answers suggested, use this: 方法2:作为其他答案建议,使用此方法:

s.useDelimiter(",");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM