Messed up CSV导致异常 [英] Messed up CSV leads to Exception

查看:381
本文介绍了Messed up CSV导致异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为我发现了一个错误。或者也许不是,但超级CSV不能处理好。



我使用MapReader解析一个包含41列的CSV文件。但是,我得到的CSV - 和Webservice,给我的CSV混乱了一行。 标题行是带有41个单元格的制表符分隔行。



错线是一个制表符分隔的36行单元格行,

这是我使用的代码:






  InputStream fis = new FileInputStream(pathToCsv); 
InputStreamReader inReader = new InputStreamReader(fis,ISO-8859-1);

ICsvMapReader mapReader = new CsvMapReader(inReader,new CsvPreference.Builder('','\t',\r\\\
)。build());
final String [] headers = mapReader.getHeader(true);
Map< String,String> row;
while((row = mapReader.read(headers))!= null){

//做某事


}






在上面提到的行中执行mapReader.read(headers)时,我得到了一个异常,这是一个例外:

  org.supercsv.exception.SuperCsvException:
nameMapping数组和sourceList应该是相同的大小(nameMapping length = 41,sourceList size = 36)
context = null
at org.supercsv.util.Util.filterListToMap(Util.java:121)
at org.supercsv.io.CsvMapReader.read(CsvMapReader.java:79)
at test.MyClass .readCSV(MyClass.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native方法)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun .reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)

你认为我应该怎么办?



我不想让整个应用程序崩溃,

解决方案

这是一个很好的问题!作为超级CSV开发人员,我将在网站上创建一些异常处理示例。



您可以保持简单,并使用CsvListReader(不关心多少列),然后自己创建Map:

  public class HandlingExceptions {

private static final String INPUT =
name\tage\\\
Tom\ t25 \\\
Alice\\\
Jim\t44\\\
Mary\t33\tInvalid;

public static void main(String [] args)throws IOException {

//使用CsvListReader(不能确定有正确的列数)
ICsvListReader listReader = new CsvListReader(new StringReader(INPUT),
new CsvPreference.Builder('','\t',\r\\\
)。build());

final String [] headers = listReader.getHeader(true);

List< String> row = null;
while((row = listReader.read())! = null){

if(listReader.length()!= headers.length){
//跳过具有无效列数的行
System.out.println(跳过无效行:+ row);
continue;
}

现在可以安全地创建映射
Map< String,String> rowMap = new HashMap< String,String>();
Util.filterListToMap(rowMap,headers,row);

//对你的地图做一些操作
System.out.println(rowMap);
}
listReader.close();
}
}

输出:


b $ b

  {name = Tom,age = 25} 
跳过无效行:[Alice]
{name = Jim,age = 44}
跳过无效行:[Mary,33,Invalid]

如果您担心使用超级CSV Util 类(它可能会改变 - 它真的是一个内部实用程序类),你可以结合两个读者,因为我建议此处



您可以尝试捕获 SuperCsvException 但是你可能最终会抑制的列数不仅仅是无效的。我建议捕获的唯一的Super CSV异常(虽然不适用于你不使用单元处理器的情况)是 SuperCsvConstraintViolationException ,因为它表明文件在正确的格式,但数据不满足您的预期约束。


I think i found a bug. Or maybe it isn't, but Super CSV can't handle that well.

I'm parsing a CSV file with 41 Columns with a MapReader. However, i'm getting that CSV - and the Webservice that gives me the CSV messes up one line. The "headline" line is a tab-delimited Row with 41 Cells.

And the "wrong line" is a tab-delimited Row with 36 Cells and the content doesn't make any sense.

This is the code i'm using:


InputStream fis = new FileInputStream(pathToCsv);
InputStreamReader inReader = new InputStreamReader(fis, "ISO-8859-1");

ICsvMapReader mapReader = new CsvMapReader(inReader, new CsvPreference.Builder('"','\t',"\r\n").build());
final String[] headers = mapReader.getHeader(true);
Map<String, String> row;
while( (row = mapReader.read(headers)) != null ) {

    // do something


}


I get an exception when executing mapReader.read(headers) in the row i mentioned above. This is the exception:

org.supercsv.exception.SuperCsvException: 
the nameMapping array and the sourceList should be the same size (nameMapping length = 41, sourceList size = 36)
context=null
at org.supercsv.util.Util.filterListToMap(Util.java:121)
at org.supercsv.io.CsvMapReader.read(CsvMapReader.java:79)
at test.MyClass.readCSV(MyClass.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)

What do you think i should do ?

I don't want the whole application to crash, just because one row is messed up, i'd rather skip that row.

解决方案

This is a good question! As a Super CSV developer, I'll look into creating some exception handling examples on the website.

You could keep it simple and use CsvListReader (which doesn't care how many columns there are), and then just create the Map yourself:

public class HandlingExceptions {

    private static final String INPUT = 
        "name\tage\nTom\t25\nAlice\nJim\t44\nMary\t33\tInvalid";

    public static void main(String[] args) throws IOException {

        // use CsvListReader (can't be sure there's the correct no. of columns)
        ICsvListReader listReader = new CsvListReader(new StringReader(INPUT), 
            new CsvPreference.Builder('"', '\t', "\r\n").build());

        final String[] headers = listReader.getHeader(true);

        List<String> row = null;
        while ((row = listReader.read()) != null) {

            if (listReader.length() != headers.length) {
                // skip row with invalid number of columns
                System.out.println("skipping invalid row: " + row);
                continue;
            }

            // safe to create map now
            Map<String, String> rowMap = new HashMap<String, String>();
            Util.filterListToMap(rowMap, headers, row);

            // do something with your map
            System.out.println(rowMap);
        }
        listReader.close();
    }
}

Output:

{name=Tom, age=25}
skipping invalid row: [Alice]
{name=Jim, age=44}
skipping invalid row: [Mary, 33, Invalid]

If you were concerned with using Super CSV's Util class (it's possible it could change - it's really an internal utility class), you could combine 2 readers as I've suggested here.

You could try catching SuperCsvException, but you might end up suppressing more than just an invalid number of columns. The only Super CSV exception I'd recommend catching (though not applicable in your situation as you're not using cell processors) is SuperCsvConstraintViolationException, as it's indicates the file is in the correct format, but the data doesn't satisfy your expected constraints.

这篇关于Messed up CSV导致异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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