2016-07-18 50 views
0
import java.io.IOException; 
import java.io.StringReader; 

import org.apache.commons.csv.CSVFormat; 
import org.apache.commons.csv.CSVParser; 

Apache CSVパーサーで単純なCSVファイルを解析しようとしています。私は引用符を使用しない限り、それは正常に動作します。私は入力Apache Commons CSV:引用入力が機能しない

"a";42 

に引用符を追加しようとすると、それは私にエラーを与える:ここで

invalid char between encapsulated token and delimiter 

は、単純な、完全なコードです:

public class Test { 

    public static void main(String[] args) throws IOException { 
     String DATA = "\"a\";12"; 
     CSVParser csvParser =  
     CSVFormat.EXCEL 
      .withIgnoreEmptyLines() 
      .withIgnoreHeaderCase() 
      .withRecordSeparator('\n').withQuote('"') 
      .withEscape('\\').withRecordSeparator(';').withTrim() 
      .parse(new StringReader(DATA)); 
    } 

} 

私は単純にすることはできません私がコードで逃したものを見つけてください。

答えて

2

問題はそれほど簡単ではありませんでした。

withDelimiterの代わりにwithRecordSeparatorを使用してフィールドセパレータを設定しました。

これは私が期待どおりに動作:

public class Test { 

    public static void main(String[] args) throws IOException { 
     String DATA = "\"a\";12"; 
     CSVParser csvParser =  
     CSVFormat.EXCEL 
      .withIgnoreEmptyLines() 
      .withIgnoreHeaderCase() 
      .withRecordSeparator('\n').withQuote('"') 
      .withEscape('\\').withDelimeter(';').withTrim() 
      .parse(new StringReader(DATA)); 
    } 

}

関連する問題