简体   繁体   中英

CsvMapReader doesn't appear to parse my file

I have a very simple tsv file, with entries as so:

614 2006-07-13 15:30:05 2009-11-20 23:56:21 510 350 3265    10  34
1038    2006-07-15 16:12:15 2009-11-16 05:12:11 304 443 4405    7   156
1437    2006-07-16 12:29:24 2009-11-16 16:25:12 45  73  725 6   37
2615    2006-07-19 23:23:55 2009-11-27 18:34:36 211 230 211 7   0
3148    2006-07-26 14:17:22 2009-11-20 17:35:18 7346    7244    11438   8   97
5593    2006-09-08 10:58:49 2009-11-24 06:08:27 898 1024    2897    8   56

It does not have headers, and I'm getting it from another source, so I have no control over how it's written. I want to read in the first column, do something with it, and ignore the rest.

My code is:

    List<Long> userIds = new ArrayList<Long>();

    ICsvMapReader mapReader =  null;
    try {
        mapReader = new CsvMapReader(new FileReader(inFile), CsvPreference.TAB_PREFERENCE);  

        // only map the first column - setting header elements to null means those columns are ignored
        final String[] header = new String[] { "userid", null, null, null, null, null, null };

        final CellProcessor[] processors = new CellProcessor[] {null,
                null,
                null,
                null,
                null,
                null,
                null };

        Map<String, Object> userMap;
        while( (userMap = mapReader.read(header, processors)) != null ) {
            Long userId = Long.parseLong(userMap.get("userid").toString());
            userIds.add(userId);
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    finally {
        IOUtils.closeQuietly(mapReader);
    }

I get no exceptions, but the mapReader.read() line always returns null. I tried using new ParseLong() in place of null in the first position of the processor, and that had no effect. I feel like I'm missing something really basic.

Your code works fine for me, although I had to add an extra null element to the header and processors as there's actually 8 columns in your data. Otherwise Super CSV throws an exception:

org.supercsv.exception.SuperCsvException: The number of columns 
to be processed (8) must match the number of CellProcessors (7): 
check that the number of CellProcessors you have defined matches 
the expected number of columns being read/written
context={lineNo=1, rowNo=1, columnNo=1, rowSource=
[614, 2006-07-13 15:30:05, 2009-11-20 23:56:21, 510, 350, 3265, 10, 34]}

I'd check that you're reading in the correct file - it sounds like you're reading in an empty file...

Use uniVocity-parsers to parse your TSV file:

TsvParserSettings parserSettings = new TsvParserSettings();
parserSettings.selectIndexes(0); //selects the first column only
TsvParser parser = new TsvParser(parserSettings);

//the rows will contain a String array of length 1, with the values of the first column only.
List<String[]> parsedRows = parser.readAll(new FileReader(yourFile));

Also, don't use a CSV parser to parse a TSV file. The parsing algorithm is not equivalent (even though it looks that way initially).

Disclosure: I am the author of this library. It's open-source and free (Apache V2.0 license).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM