Using SuperCsv with Multiple Variable Columns

I watched this example from the Super CSV website which shows that the date of birth is an optional column. What happens if I have a few extra 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(); } } } 

Also, if the extra columns are not at the end, but in the center or somewhere else?

+4
source share
1 answer

So, the real problem is that in order to use the correct cell processors, you need to know what data in each column. With a valid CSV file (the same number of columns on each row) this is not a problem, but if you are dealing with a CSV file with a variable column, it is difficult.

If, as in the example, only one column is optional, you just need to count the number of columns read and use the appropriate array of cell processors. It doesn't matter where this optional column is, because it is still predictable.

If, however, more than one column is optional, you have a problem. For example, if middleName and city are optional in the following CSV file:

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

This can be read as:

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

or

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

This is no longer predictable. You can check the data in a column to determine what that column should represent (for example, the date has a / ), but it is not very reliable, and even then you might even need to read a few lines in order to figure it out.

+2
source

Source: https://habr.com/ru/post/1496708/


All Articles