使用SuperCsv与多个可变列 [英] Using SuperCsv with multiple variable columns

查看:327
本文介绍了使用SuperCsv与多个可变列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从超级CSV 网站查看此示例,其中显示dateofbirth是可选列。如果我有多个可选列会发生什么?代码如何改变?

I was looking at this example from the Super CSV website which shows that dateofbirth is optional column. What happens if i have more than one optional columns? How will the code change than?

 private static void readVariableColumnsWithCsvListReader() throws Exception {

        final CellProcessor[] allProcessors = new CellProcessor[] { new UniqueHashCode(), // customerNo (must be unique)
                new NotNull(), // firstName
                new NotNull(), // lastName
                new ParseDate("dd/MM/yyyy") }; // birthDate

        final CellProcessor[] noBirthDateProcessors = new CellProcessor[] { allProcessors[0], // customerNo
                allProcessors[1], // firstName
                allProcessors[2] }; // lastName

        ICsvListReader listReader = null;
        try {
                listReader = new CsvListReader(new FileReader(VARIABLE_CSV_FILENAME), CsvPreference.STANDARD_PREFERENCE);

                listReader.getHeader(true); // skip the header (can't be used with CsvListReader)

                while( (listReader.read()) != null ) {

                        // use different processors depending on the number of columns
                        final CellProcessor[] processors;
                        if( listReader.length() == noBirthDateProcessors.length ) {
                                processors = noBirthDateProcessors;
                        } else {
                                processors = allProcessors;
                        }

                        final List<Object> customerList = listReader.executeProcessors(processors);
                        System.out.println(String.format("lineNo=%s, rowNo=%s, columns=%s, customerList=%s",
                                listReader.getLineNumber(), listReader.getRowNumber(), customerList.size(), customerList));
                }

        }
        finally {
                if( listReader != null ) {
                        listReader.close();
                }
        }
}

推荐答案

因此,这里真正的问题是应用正确的单元处理器,您需要知道每列中有什么数据。有了一个有效的CSV文件(每行的列数相同),这不是一个问题,但如果你处理的是一个变量列CSV文件,它是棘手的。

So the real issue here is that to apply the correct cell processors, you need to know what data is in each column. With a valid CSV file (same no. of columns on each line) that's not a problem, but if you're dealing with a variable column CSV file it's tricky.

如果,像例子一样,只有1列是可选的,那么你只需要计算读取的列数,并使用适当的单元处理器数组。

If, like the example, only 1 column is optional then you just need to count the number of columns read and use the appropriate array of cell processors. It doesn't matter where that optional column is, because it's still predictable.

但是,如果多于一列是可选的,那么,重新麻烦。例如,如果 middleName city 在以下CSV文件中是可选的:

If, however, more than 1 column is optional you're in trouble. For example, if middleName and city are optional in the following CSV file:

firstName,middleName,lastName,city
Philip,Fry,New York

可以读作:

firstName="Philip", middleName="Fry", lastName="New York", city=null

firstName="Philip", middleName=null, lastName="Fry", city="New York"

它不再是可预测的。您可以检查列中的数据以确定该列应该代表什么(例如日期具有 / ),但是这不是非常鲁棒,甚至你甚至可能需要读几行,以便弄清楚。

It's no longer predictable. You may be able to inspect the data in the column to determine what that column should represent (e.g. a date has /'s), but that's not very robust, and even then you may even have to read a few lines in order to figure it out.

这篇关于使用SuperCsv与多个可变列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆