0

我必须分析其具有可看起来像以下字段的CSV文件:忽略Apache Commons CSV/OpenCSV中引用字段中的分隔符?

("FOO, BAR BAZ", 42) 

和产量两个领域:

FOO, BAR BAZ 
42 

我不知道如何做到这一点用简洁Apache Commons CSV或OpenCSV,所以我正在寻找一些指导。这可能只是因为我并不完全了解org.apache.commons.csv.CSVFormat属性“quoteChar”which is touched on in the documentation,但从未在任何地方明确解释过我能找到的内容。如果是这样,如果您能指出我对该功能的更好文档,这将非常有帮助。

这里有一个简单的例子,显示我的问题,还有什么我已经试过,结果:

 String test = "(\"FOO, BAR BAZ\", 42)"; 
     int numTries = 5; 
     CSVParser[] tries = new CSVParser[numTries]; 
     tries[0] = CSVParser.parse(line, CSVFormat.DEFAULT.withRecordSeparator("\n"));//BAR BAZ" 
     tries[1] = CSVParser.parse(line, CSVFormat.DEFAULT.withQuote('"'));//BAR BAZ" 
     tries[2] = CSVParser.parse(line, CSVFormat.DEFAULT.withQuote(null));//BAR BAZ" 
     tries[3] = CSVParser.parse(line, CSVFormat.DEFAULT.withQuote('"').withQuoteMode(QuoteMode.NON_NUMERIC));//BAR BAZ" 
     tries[4] = CSVParser.parse(line, CSVFormat.DEFAULT.withRecordSeparator(")\n("));//BAR BAZ" 

     for(int i = 0; i < numTries; i++){ 
      CSVRecord record = tries[i].getRecords().get(0); 
      System.out.println(record.get(1));//.equals("42")); 
     } 

需要注意的是,如果你排除输入括号正常工作。

回答

0

您可以使用OpenCSVCSVReader读取数据并获取数据元素,如下图所示:

public static void main(String[] args) { 
    try(FileReader fr = new FileReader(new File("C:\\Sample.txt")); 
       CSVReader csvReader = new CSVReader(fr);) { 
      String[] data = csvReader.readNext(); 
      for(String data1 : data) { 
       System.out.println(data1); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
+0

它会忽略其用引号括起来的分隔符默认? –

+0

因为这就是我的问题所在。 –

0

对我来说公地CSV默认的格式不正确的事情正确格式化CSV消息:

Reader in = new StringReader("\"FOO, BAR BAZ\", 42"); 
    Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(in); 
    for (CSVRecord record : records) { 
     for(int i = 0;i < record.size();i++) { 
      System.out.println("At " + i + ": " + record.get(i)); 
     } 
    } 

引出:

At 0: FOO, BAR BAZ 
At 1: 42 

对于特殊格式的线,你可能需要做更多的处理顶部删除这些括号:

BufferedReader lineReader = new BufferedReader(
      new StringReader("(\"FOO, BAR BAZ\", 42)\n(\"FOO, BAR FOO\", 44)")); 

    while(true) { 
     String line = lineReader.readLine(); 
     if (line == null) { 
      break; 
     } 

     String adjustedLine = line.substring(1, line.length() - 1); 
     records = CSVFormat.DEFAULT.parse(new StringReader(adjustedLine)); 
     for (CSVRecord record : records) { 
      for (int i = 0; i < record.size(); i++) { 
       System.out.println("At " + i + ": " + record.get(i)); 
      } 
     } 
    } 
+0

这就是我所期望的,但是我得到'封装的令牌和分隔符之间的'无效字符和解决方案[这里](http://stackoverflow.com/questions/26729799/invalid-char-between-encapsulated-token-and-delimiter -in-apache-commons-csv-libr)建议更改_withQuote_来修复它。 –

+0

可以在你的问题中包含一个最小的测试用例吗?因为我没有看到你的代码不同,所以我的工作原理与我发布的完全一样,并且不会报告任何错误。至少要检查文字中的哪些引用,如果它们是“印刷引号”,则可能会失败,例如在Word中添加。 – centic

+0

啊,我看到了问题。您的输入行周围没有带圆括号的圆括号。可能是我的错 - 尽管他们最初被排除在外了。不过,我添加了一个测试用例。 –

0

您可以opencsv实现这一目标如下:

import com.opencsv.CSVReader; 
import java.io.FileReader; 
import java.io.IOException; 

public class NewClass1 { 
    public static void main(String[] args) throws IOException { 
     String fileName = "C:\\yourFile.csv"; 
     String [] nextLine; 
     // use the three arg constructor to tell the reader which delimiter you have in your file(2nd arg : here ',')               
     // you can change this to '\t' if you have tab separeted file or ';' or ':' ... whatever your delimiter is 
     // (3rd arg) '"' if your fields are double quoted or '\'' if single quoted or no 3rd arg if the fields are not quoted 
     CSVReader reader = new CSVReader(new FileReader(fileName), ',' ,'"'); 
     // nextLine[] is an array of values from the line 
     // each line represented by String[], and each field as an element of the array 
     while ((nextLine = reader.readNext()) != null) {   
      System.out.println("nextLine[0]: " +nextLine[0]); 
      System.out.println("nextLine[1]: " +nextLine[1]); 
     } 
    } 
} 
+0

即使只有一些字段(即字符串字段)被引用? –

相关问题