Example usage for org.apache.commons.csv CSVParser getRecords

List of usage examples for org.apache.commons.csv CSVParser getRecords

Introduction

In this page you can find the example usage for org.apache.commons.csv CSVParser getRecords.

Prototype

public List<CSVRecord> getRecords() throws IOException 

Source Link

Document

Parses the CSV input according to the given format and returns the content as a list of CSVRecord CSVRecords .

Usage

From source file:de.dhbw.vetaraus.CSV.java

/**
 * Create a list of Case objects from a given filepath. The filepath must be a CSV file with semicolon-delimited
 * entries. The first line of the file (header record) will be ignored.
 * <p>/*from  w  w w.  ja  v  a2  s  .c  o m*/
 * Each line of the file must have the following values:
 * ID, age, gender, married, children, degree, occupation, income, tariff.
 *
 * @param path
 *         Path to the CSV file.
 * @return a list of Case objects from the given input file.
 * @throws IOException
 */
public static List<Case> parse(String path) throws IOException {
    try (FileInputStream fis = new FileInputStream(path);
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader file = new BufferedReader(isr)) {

        final CSVParser parser = new CSVParser(file,
                CSVFormat.DEFAULT.withDelimiter(';')
                        .withHeader(Constants.HEADER_NUMBER, Constants.HEADER_AGE, Constants.HEADER_GENDER,
                                Constants.HEADER_MARRIED, Constants.HEADER_CHILDREN, Constants.HEADER_DEGREE,
                                Constants.HEADER_OCCUPATION, Constants.HEADER_INCOME, Constants.HEADER_TARIFF)
                        .withSkipHeaderRecord(true));

        return parser.getRecords().stream()
                .map(record -> new Case(record.get(Constants.HEADER_NUMBER), record.get(Constants.HEADER_AGE),
                        record.get(Constants.HEADER_GENDER), record.get(Constants.HEADER_MARRIED),
                        record.get(Constants.HEADER_CHILDREN), record.get(Constants.HEADER_DEGREE),
                        record.get(Constants.HEADER_OCCUPATION), record.get(Constants.HEADER_INCOME),
                        record.get(Constants.HEADER_TARIFF)))
                .collect(Collectors.toList());
    }
}

From source file:citation_prediction.CitationCore.java

/**
 * Get CSV citation history from a file.
 * //from  www  . j a  va2  s . c o m
 * @param filename The filename and path containing the citation data.
 * @param format The format of the file.
 * @param hasHeader Does the file have a line with headings?
 * @return A record containing the csv information.
 */
private static List<CSVRecord> getCSVData(String filename, CSVFormat format, boolean hasHeader) {

    boolean error = true;
    List<CSVRecord> list_ourdata = null;

    try {
        FileReader ourdata = new FileReader(filename);

        CSVParser data_parser = new CSVParser(ourdata, format);

        list_ourdata = data_parser.getRecords();

        if (hasHeader) {
            list_ourdata.remove(0);
        } //remove header file.

        Iterator<CSVRecord> list_iterator = list_ourdata.iterator();
        for (int rowIndex = 0; rowIndex < list_ourdata.size(); rowIndex++) {
            CSVRecord record = list_iterator.next();

            System.out.println("#" + (rowIndex + 1) + " " + record.toString());

        }

        data_parser.close();
        ourdata.close();

        error = false;

    } catch (java.io.FileNotFoundException e) {
        System.err.println("ERROR: There was an error opening, reading, or parsing the input file.");
        System.err.println("ERROR:" + filename);
        error = true;
    } catch (java.io.IOException e) {
        System.err.println("ERROR: Could not close the parser or the input file.");
        error = true;
    }

    if (error || list_ourdata == null) {
        System.exit(1);
        return null;
    } else {
        return list_ourdata;
    }
}

From source file:Client.Message.java

public Message(String message) throws IOException {
    this.message = message;
    Reader in = new StringReader(message);
    CSVParser parser = new CSVParser(in, CSVFormat.DEFAULT);
    List<CSVRecord> list = parser.getRecords();

    type = MessageType.valueOf(list.get(0).toString());
    author = list.get(1).toString();/* w  w  w.  java2 s  .c om*/
    if (type == MessageType.TEXT) {
        text = list.get(2).toString();
    }
}

From source file:com.publictransitanalytics.scoregenerator.datalayer.directories.GTFSReadingTripDetailsDirectory.java

private void parseTripsFile(final Reader tripReader) throws IOException, InterruptedException {

    final CSVParser tripParser = new CSVParser(tripReader, CSVFormat.DEFAULT.withHeader());

    final List<CSVRecord> tripRecords = tripParser.getRecords();
    for (CSVRecord record : tripRecords) {
        final String rawTripId = record.get("trip_id");
        final String routeId = record.get("route_id");
        final String serviceType = record.get("service_id");
        populateTripDetail(rawTripId, routeId, serviceType);
    }/*  w w w.  j  ava  2  s.c om*/
}

From source file:edu.emory.mathcs.nlp.zzz.CSVRadiology.java

public void categorize(String inputFile) throws Exception {
    CSVParser parser = new CSVParser(IOUtils.createBufferedReader(inputFile), CSVFormat.DEFAULT);
    List<CSVRecord> records = parser.getRecords();
    StringJoiner join;//from  w w w  . j  a  va2  s.  c  o  m
    CSVRecord record;

    for (int i = 0; i <= 500; i++) {
        if (i == 0)
            continue;
        record = records.get(i);
        join = new StringJoiner(" ");

        for (int j = 2; j < 7; j++)
            join.add(record.get(j));

        System.out.println(join.toString());
    }

    parser.close();
}

From source file:edu.emory.mathcs.nlp.zzz.CSVSentiment.java

public void categorize(String inputFile) throws Exception {
    CSVParser parser = new CSVParser(IOUtils.createBufferedReader(inputFile), CSVFormat.DEFAULT);
    List<CSVRecord> records = parser.getRecords();
    List<NLPNode[]> document;
    String outputDir;//from  w  w w  .j  a v  a 2s.c o  m
    PrintStream fout;
    CSVRecord record;

    System.out.println(inputFile);

    for (int i = 0; i < records.size(); i++) {
        if (i == 0)
            continue;
        record = records.get(i);
        document = decode.decodeDocument(record.get(6));
        document.get(0)[1].putFeat("sent", record.get(0));

        outputDir = inputFile.substring(0, inputFile.length() - 4);
        fout = IOUtils.createBufferedPrintStream(
                outputDir + "/" + FileUtils.getBaseName(outputDir) + "_" + i + ".nlp");
        for (NLPNode[] nodes : document)
            fout.println(decode.toString(nodes) + "\n");
        fout.close();
    }

    parser.close();
}

From source file:com.publictransitanalytics.scoregenerator.datalayer.directories.GTFSReadingStopDetailsDirectory.java

public GTFSReadingStopDetailsDirectory(final Store<StopIdKey, StopDetails> stopDetailsStore,
        final Reader stopDetailsReader) throws IOException, InterruptedException {

    this.stopDetailsStore = stopDetailsStore;
    try {//from  w w w.j  av a2s.c o m
        if (stopDetailsStore.isEmpty()) {
            log.info("Building stop details directory.");

            final CSVParser parser = new CSVParser(stopDetailsReader, CSVFormat.DEFAULT.withHeader());
            final List<CSVRecord> stopDetailsRecords = parser.getRecords();
            for (CSVRecord record : stopDetailsRecords) {
                final String latitude = record.get("stop_lat");
                final String longitude = record.get("stop_lon");
                final String stopId = record.get("stop_id");
                final StopDetails stopDetails = new StopDetails(stopId, record.get("stop_name"),
                        new Coordinate(latitude, longitude));
                stopDetailsStore.put(new StopIdKey(stopId), stopDetails);
            }
        }
    } catch (final BitvantageStoreException e) {
        throw new ScoreGeneratorFatalException(e);
    }
}

From source file:io.github.seiferma.jameica.hibiscus.dkb.creditcard.synchronize.csvparser.DKBCsvParser.java

public Date getBalanceDate() throws IOException {
    CSVParser parser = CSVParser.parse(metaDataCsv, csvFormat);
    Optional<String> foundDate = parser.getRecords().stream().filter(r -> "Datum:".equals(r.get(0)))
            .map(r -> r.get(1)).findFirst();
    if (!foundDate.isPresent()) {
        throw new IOException("Could not find balance date.");
    }//from  w w w  . j  a v a  2  s  .  c o  m
    try {
        return DateUtils.parseDate(foundDate.get());
    } catch (ParseException e) {
        throw new IOException("Could not parse balance date.", e);
    }
}

From source file:io.github.seiferma.jameica.hibiscus.dkb.creditcard.synchronize.csvparser.DKBCsvParser.java

public int getBalanceInCents() throws IOException {
    CSVParser parser = CSVParser.parse(metaDataCsv, csvFormat);
    List<CSVRecord> records = parser.getRecords();
    Optional<String> foundSaldo = records.stream().filter(r -> "Saldo:".equals(r.get(0))).map(r -> r.get(1))
            .findFirst();/*from  w w w.j  a  v a2s .  co m*/
    if (!foundSaldo.isPresent()) {
        throw new IOException("Could not find balance record.");
    }
    Pattern saldoPattern = Pattern.compile("(-?[0-9]+)([.]([0-9]{1,2}))? EUR");
    Matcher saldoMatcher = saldoPattern.matcher(foundSaldo.get());
    if (!saldoMatcher.matches()) {
        throw new IOException("Could not parse saldo.");
    }

    String euroString = saldoMatcher.group(1);
    String centString = saldoMatcher.group(3) == null ? "0" : saldoMatcher.group(3);

    return getCentsFromString(euroString, centString);
}

From source file:co.cask.hydrator.transforms.ParseCSV.java

@Override
public void transform(StructuredRecord in, Emitter<StructuredRecord> emitter) throws Exception {
    // Field has to string to be parsed correctly. For others throw an exception.
    String body = in.get(config.field);

    // Parse the text as CSV and emit it as structured record.
    try {/*www .  j  av  a 2s .  c  om*/
        CSVParser parser = CSVParser.parse(body, csvFormat);
        List<CSVRecord> records = parser.getRecords();
        for (CSVRecord record : records) {
            if (fields.size() == record.size()) {
                StructuredRecord sRecord = createStructuredRecord(record);
                emitter.emit(sRecord);
            } else {
                LOG.warn("Skipping record as ouput schema specified has '{}' fields, while CSV record has '{}'",
                        fields.size(), record.size());
                // Write the record to error Dataset.
            }
        }
    } catch (IOException e) {
        LOG.error("There was a issue parsing the record. ", e.getLocalizedMessage());
    }
}