0
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();
}





}

我在这个语法中缺少什么,或者有没有更好的方法来完成这个任务

4

3 回答 3

1

有几个问题:

  1. ParseIntParseDouble分别用于将CSV 字符串Integer入和Double请参阅此处的方便表,了解哪些单元处理器可用于读/写或两者兼而有之。如果你愿意,你可以留下它们null,Super CSV 只会调用toString()每个对象。

  2. 您得到的异常(1 列 / 8 个处理器)表明您希望有 8 列(即列表中的 8 个元素),但只有 1 个。您只传递一个字符串Arrays.asList()- 看起来像您假设此方法实际上将 String 拆分为 List (它没有!)。

  3. 为什么要将租金数据列表转换为字符串?这真的很奇怪。如果您的数据需要任何操作(我不确定是否需要),那么您应该更新 List 中的每个元素,而不是将整个 List 转换为 String 然后尝试再次拆分它。

  4. 当您不进行任何替换时输出是什么,即当您将rentData列表直接传递给时会发生什么listWriter.write()

我可以建议您修复处理器设置(将ParseIntandParseDouble替换为null),将rentDataList 直接传递给 Super CSV ...

listWriter.write(rentData, processors);

...然后将结果(输出/堆栈跟踪)发布到您的问题。

于 2013-08-06T22:39:26.610 回答
0

这个问题的答案在于问题。

要处理的列数 (1) 必须与 CellProcessor 的数量 (8) 匹配:

实际上,您连续拥有的逗号分隔数据数量比您最初告诉 SuperCSV 的要多。

final String [] header = new String []{"_num", "End_Date", "bal_val","rval","cval","bf_val","aval","pval"};

这使 SuperCSV 假设每行和每列只有 8 个值对应一个标题。如果你在行中传递更多的值,它不知道那个值对应的是什么,所以它会抛出异常。

此链接为您提供如何定义可选/必填列。

于 2013-08-06T08:49:57.243 回答
0

@Hound Dog虽然你可能是对的,但我无法让它按照我自己想要的方式工作。我将列表更改为字符串类型并且仍然得到那个 [lorg supercsv] 废话,我只是决定退出超级 csv 的事情,以防万一有人遇到这个问题,这样做我发现这样做更容易。除了 generate 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

}

}

于 2013-08-16T12:46:35.913 回答