Exception in thread "main" org.supercsv.exception.SuperCsvException: The number of columns to be processed (229326) must match the number of CellProcessors (8):
我相信我可能不得不重做使用supercsv的操作,因为从长远来看它可能更容易,但是我可以接受任何其他建议。我只是想写回一个csv文件,我有一个包含所有数据的列表但是输出就像这样
4350 02/06/2013 3965.21 0.0 0.0 0.0 0.0 0.0,
4698 02/06/2013 498.16 0.0 0.0 0.0 0.0 0.0,
4992 02/06/2013 97.87 87.82 6.05 0.0 0.0 0.0,
4441 02/06/2013 18.8 71.98 11.6 0.0 0.0 -42.5, 54092 02/06/2013 105.11 118.82 6.24 0.0 0.0 0.0,
我已经设法通过替换列表中的字符串来获得我想要的输出但是当它运行时它挂起并且我相信它是由于我如何写回csv,我不确定,还有什么除了将它写回csv而不是使用超级csv之外做其他事情。我得到的错误是
"1805","31/07/2013","-233.4","0.0","0.0","0.0","0.0","0.0"
"8054","31/07/2013","280.45","82.38","52.38","0.0","0.0","-200.0"The number of columns to be processed (1) must match the number of CellProcessors (8):
我的witer类如下
package writer;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.supercsv.cellprocessor.FmtDate;
import org.supercsv.cellprocessor.ParseDouble;
import org.supercsv.cellprocessor.ParseInt;
import org.supercsv.cellprocessor.constraint.NotNull;
import org.supercsv.cellprocessor.ift.CellProcessor;
import org.supercsv.io.CsvListWriter;
import org.supercsv.io.ICsvListWriter;
import org.supercsv.prefs.CsvPreference;
public class RentStatementsWriter {
public ArrayList rData;
private List<String> csvData;
char b = ',';
public RentStatementsWriter(ArrayList rentData) {
rData = rentData;
ICsvListWriter listWriter = null;
try{
listWriter = new CsvListWriter(new FileWriter("rentl.csv"),CsvPreference.STANDARD_PREFERENCE);
CellProcessor[] processors =new CellProcessor[]{
new ParseInt(),
new FmtDate("dd/MM/yyyy"),//
new ParseDouble(),
new ParseDouble(),
new ParseDouble(),
new ParseDouble(),
new ParseDouble(),
new ParseDouble(),
};
final String [] header = new String []{"_num", "End_Date", "bal_val","rval","cval","bf_val","aval","pval"};
System.out.print("to string "+rData.toString().replaceFirst(" ", "\"").replaceAll("\\,"," \\,").replaceAll(" ", "").replaceAll(" ", "\"\\,\"").replaceAll("\"\\,\"\\,", "\"\n\""));
csvData = Arrays.asList(rData.toString().replaceFirst(" ", "\"").replaceAll("\\,"," \\,").replaceAll(" ", "").replaceAll(" ", "\"\\,\"").replaceAll("\"\\,\"\\,", "\"\""));
/*
* replace
* .replaceAll(" ", "\"\\,\"")
*/
listWriter.writeHeader(header);
listWriter.write(csvData, processors);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print(e+" file unable to write");
} finally {
if(listWriter !=null){
try {
listWriter.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("list writer");
}
}
}
}
String listToCsv(List<String> listOfStrings, char separator) {
StringBuilder sb = new StringBuilder();
// all but last
for(int i = 0; i < listOfStrings.size() - 1 ; i++) {
sb.append("\""+listOfStrings.get(i)+"\"");
sb.append(separator);
}
// last string, no separator
sb.append(listOfStrings.get(listOfStrings.size()-1));
return sb.toString();
}
}
我在这种语法中缺少什么,或者有更好的方法来完成这项任务
答案 0 :(得分:1)
有几个问题:
ParseInt
和ParseDouble
分别用于读取 CSV字符串到Integer
和Double
。请参阅方便的表here以查看哪些单元处理器可用于读/写或两者。如果需要,您可以留下null
,而Super CSV只会在每个对象上调用toString()
。
您获得的异常(1列/ 8个处理器)表示您期望有8列(即列表中的8个元素),但只有1个。您只是通过了单个字符串到Arrays.asList()
- 看起来你假设这个方法实际上将String拆分成一个List(它没有!)。
为什么要将租借数据列表转换为字符串?这真是奇怪。如果您的数据需要任何操作(我不确定它是否存在),那么您应该更新List中的每个元素,而不是将整个List转换为String然后再尝试将其拆分。
不进行任何替换时的输出是什么,即将rentData
列表直接传递给listWriter.write()
时会发生什么?
我可以建议您修复处理器设置(将ParseInt
和ParseDouble
替换为null
),将rentData
列表直接传递给超级CSV ... < / p>
listWriter.write(rentData, processors);
...然后将结果(输出/堆栈跟踪)发布到您的问题中。
答案 1 :(得分:0)
这个问题的解决方案就在于这个问题。
要处理的列数(1)必须与数量匹配 CellProcessors(8):
您实际上连续逗号分隔数据的数量超过了您最初告诉SuperCSV的数量。
final String [] header = new String []{"_num", "End_Date", "bal_val","rval","cval","bf_val","aval","pval"};
这使得SuperCSV假设每行和每列预期只有8个值对应于一个头。如果您在行中传递更多值,则它不知道该值对应的是什么,因此它会抛出异常。
This link为您提供了如何定义可选/必填列。
答案 2 :(得分:0)
@Hound Dog虽然你可能是对的但我无法按照我自己想要的方式工作。我将列表更改为类型字符串并且stil得到那个[lorg supercsv]废话,我只是决定退出超级csv的事情,以防万一有人遇到这个问题这样做我觉得这样更容易。除了生成csv方法
之外,不需要构造函数中的东西package writer;
public class RentStatementsWriter {
/*
*
* problem being that for super csv to work each entry will need a seperate list
*/
public ArrayList<RentStatements> rData;
private List<String> csvData;
char b = ',';
public RentStatementsWriter(ArrayList rentData) {
rData = rentData;
csvData = new ArrayList<String>();
try{
generateCsvFile("renttest.csv", rentData,b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print(e+" file unable to write");
} finally {
if(listWriter !=null){
try {
listWriter.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("list writer");
}
}
}
}
private void generateCsvFile(String fileName, ArrayList rentData, char b2) {
try{
final String [] header = new String []{"tncy_num", "End_Date", "bal_val","rent_val","chg_val","benf_val","adj_val","pay_val"};
FileWriter writer = new FileWriter(fileName);
writer.append("tncy_num");
writer.append(',');
writer.append("End_Date");
writer.append(',');
writer.append("bal_val");
writer.append(',');
writer.append("rent_val");
writer.append(',');
writer.append("chg_val");
writer.append(',');
writer.append("benf_val");
writer.append(',');
writer.append("adj_val");
writer.append(',');
writer.append("pay_val");
writer.append('\n');
for(int i = 0;i <rentData.size();i++){
String line = rentData.get(i).toString();
String bits []=line.split(" ");//splits each space into diffrent bits
//string something = bits.get waleva the it is surround it by ""
writer.append(bits[1]);
writer.append(b2);
writer.append(bits[2]);
writer.append(b2);
writer.append(bits[3]);
writer.append(b2);
writer.append(bits[4]);
writer.append(b2);
writer.append(bits[5]);
writer.append(b2);
writer.append(bits[6]);
writer.append(b2);
writer.append(bits[7]);
writer.append(b2);
writer.append(bits[8]);
writer.append('\n');
}
writer.flush();
writer.close();
}catch(IOException e)
{
e.printStackTrace();
}
// TODO Auto-generated method stub
}
}