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

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

Introduction

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

Prototype

public CSVParser(final Reader reader, final CSVFormat format) throws IOException 

Source Link

Document

Customized CSV parser using the given CSVFormat

If you do not read all records from the given reader , you should call #close() on the parser, unless you close the reader .

Usage

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);
    }//ww  w .jav a  2 s  . c  o m
}

From source file:br.edimarmanica.trinity.check.CheckAttributeNotFound.java

private void readOffSets() {
    /**/*  w  w  w  .j  av  a  2 s  .com*/
     * Lendos os Run02.NR_SHARED_PAGES primeiros elementos de cada offset
     */
    File dir = new File(Paths.PATH_TRINITY + site.getPath() + "/offset");

    for (int nrOffset = 0; nrOffset < dir.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.endsWith(".csv");
        }
    }).length; nrOffset++) {
        List<Map<String, String>> offset = new ArrayList<>(); //cada arquivo  um offset

        try (Reader in = new FileReader(dir.getAbsoluteFile() + "/result_" + nrOffset + ".csv")) {
            try (CSVParser parser = new CSVParser(in, CSVFormat.EXCEL)) {
                int nrRegistro = 0;
                for (CSVRecord record : parser) {

                    for (int nrRegra = 0; nrRegra < record.size(); nrRegra++) {
                        String value;
                        try {
                            value = formatValue(Preprocessing.filter(record.get(nrRegra)));
                        } catch (InvalidValue ex) {
                            value = "";
                        }

                        if (nrRegistro == 0) {
                            Map<String, String> regra = new HashMap<>();
                            regra.put(record.get(0), value);
                            offset.add(regra);
                        } else {
                            offset.get(nrRegra).put(record.get(0), value);
                        }
                    }
                    nrRegistro++;
                }
            }
            offsets.add(offset);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(CheckAttributeNotFound.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(CheckAttributeNotFound.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

From source file:com.gs.obevo.db.impl.core.changetypes.CsvReaderDataSource.java

/**
 * Putting this init here so that we can discover the file fields before running the actual rec
 *///  w  ww .ja va2  s .  c om
public void init() {
    if (!this.initialized) {
        try {
            MutableList<String> fields;
            if (csvVersion == CsvStaticDataReader.CSV_V2) {
                CSVFormat csvFormat = getCsvFormat(delim, nullToken);
                this.csvreaderV2 = new CSVParser(reader, csvFormat);
                this.iteratorV2 = csvreaderV2.iterator();
                fields = ListAdapter.adapt(IteratorUtils.toList(iteratorV2.next().iterator()));
            } else {
                this.csvreaderV1 = new au.com.bytecode.opencsv.CSVReader(this.reader, this.delim);
                fields = ArrayAdapter.adapt(this.csvreaderV1.readNext());
            }

            this.fields = fields.collect(this.convertDbObjectName);
        } catch (Exception e) {
            throw new DeployerRuntimeException(e);
        }
        this.initialized = true;
    }
}

From source file:com.qwazr.library.csv.CSVTool.java

@JsonIgnore
public CSVParser getNewParser(CSVFormat format, Reader reader, IOUtils.CloseableContext closeable)
        throws IOException {
    CSVParser parser = new CSVParser(reader, format);
    if (closeable != null)
        closeable.add(parser);//  ww  w .  ja va 2 s  .  co  m
    return parser;
}

From source file:licenseUtil.LicensingList.java

public void readFromSpreadsheet(String spreadsheetFN) throws IOException, IncompleteLicenseObjectException {
    logger.info("read spreadsheet from \"" + spreadsheetFN + "\"");
    InputStreamReader inputStreamReader = null;
    try {//from   w w w .java2s. com
        inputStreamReader = new InputStreamReader(new FileInputStream(spreadsheetFN), "UTF-8");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

    CSVParser parser = new CSVParser(bufferedReader,
            CSVFormat.DEFAULT.withHeader().withDelimiter(columnDelimiter));

    for (CSVRecord record : parser) {
        add(new LicensingObject(record));
    }
}

From source file:de.tudarmstadt.ukp.dkpro.tc.svmhmm.report.SVMHMMBatchCrossValidationReport.java

protected void aggregateResults(String testTaskCSVFile, String outputPrefix) throws Exception {
    StorageService storageService = getContext().getStorageService();

    // aggregate rows from all CSVs from all folds
    List<List<String>> allOutcomes = new ArrayList<>();

    List<TaskContextMetadata> testTasks = collectTestTasks();

    // we need test tasks!
    if (testTasks.isEmpty()) {
        throw new IllegalStateException("No test tasks found. Make sure you properly "
                + "define the test task in getTestTaskClass() (currently: " + getTestTaskClass().getName());
    }//  w  ww .  j  ava  2 s.c  o  m

    // iterate over all sub tasks
    for (TaskContextMetadata subContext : testTasks) {
        // locate CSV file with outcomes (gold, predicted, token, etc.)
        File csvFile = storageService.getStorageFolder(subContext.getId(),
                Constants.TEST_TASK_OUTPUT_KEY + File.separator + testTaskCSVFile);

        // load the CSV
        CSVParser csvParser = new CSVParser(new FileReader(csvFile), CSVFormat.DEFAULT.withCommentMarker('#'));

        // and add the all rows
        for (CSVRecord csvRecord : csvParser) {
            // row for particular instance
            List<String> row = new ArrayList<>();
            for (String value : csvRecord) {
                row.add(value);
            }
            allOutcomes.add(row);
        }

        IOUtils.closeQuietly(csvParser);
    }

    // store aggregated outcomes again to CSV
    File evaluationFile = new File(getContext().getStorageLocation(Constants.TEST_TASK_OUTPUT_KEY,
            StorageService.AccessMode.READWRITE), testTaskCSVFile);
    log.debug("Evaluation file: " + evaluationFile.getAbsolutePath());

    CSVPrinter csvPrinter = new CSVPrinter(new FileWriter(evaluationFile), SVMHMMUtils.CSV_FORMAT);
    csvPrinter.printComment(SVMHMMUtils.CSV_COMMENT);
    csvPrinter.printRecords(allOutcomes);
    IOUtils.closeQuietly(csvPrinter);

    // compute confusion matrix
    ConfusionMatrix cm = new ConfusionMatrix();

    for (List<String> singleInstanceOutcomeRow : allOutcomes) {
        // first item is the gold label
        String gold = singleInstanceOutcomeRow.get(0);
        // second item is the predicted label
        String predicted = singleInstanceOutcomeRow.get(1);

        cm.increaseValue(gold, predicted);
    }

    // and write all reports
    SVMHMMUtils.writeOutputResults(getContext(), cm, outputPrefix);

    // and print detailed results
    log.info(outputPrefix + "; " + cm.printNiceResults());
    log.info(outputPrefix + "; " + cm.printLabelPrecRecFm());
}

From source file:de.upb.wdqa.wdvd.processors.decorators.GeolocationFeatureProcessor.java

@Override
public void startRevisionProcessing() {
    logger.debug("Starting...");
    try {/*from w  w w. ja  va  2  s .c o m*/
        BufferedReader csvReader;

        csvReader = new BufferedReader(
                new InputStreamReader(
                        new BZip2CompressorInputStream(
                                new BufferedInputStream(new FileInputStream(geolocationFeatureFile))),
                        "UTF-8"));

        csvParser = new CSVParser(csvReader, CSVFormat.RFC4180.withHeader());
        iterator = csvParser.iterator();

        processor.startRevisionProcessing();

    } catch (IOException e) {
        logger.error("", e);
    }
}

From source file:loternik.MyService.java

public ArrayList<CSVRecord> getRecordsByName(String name, String okreg)
        throws FileNotFoundException, IOException {
    FileReader file = new FileReader("kandydaci.csv");
    CSVParser parser = new CSVParser(file, CSVFormat.EXCEL.withHeader().withDelimiter(';'));
    ArrayList<CSVRecord> output = new ArrayList<CSVRecord>();
    for (CSVRecord csvRecord : parser) {
        if (name.equals(csvRecord.get("Kandydat_do")) && okreg.equals(csvRecord.get("Okreg"))) {
            output.add(csvRecord);/*w w  w.j  a  v  a2  s .c o m*/
        }
    }
    return output;
}

From source file:com.x460dot10.b.registrar.StartupManager.java

/**
 * Imports data/students.txt into <code>University.students</code>
 *
 * @return             Indicates import of students was successful
 * @throws IOException //from  www  . ja  va  2 s . com
 */
public boolean importStudents() throws IOException {
    Boolean importStudentsSuccessful = true;
    File file = new File("data/mockstudents.dat");
    FileReader reader = null;
    ArrayList<MockStudent> fileStudents = new ArrayList<MockStudent>();
    Object nextStudent;
    try {
        reader = new FileReader(file);
        CSVFormat format = CSVFormat.DEFAULT;
        List<CSVRecord> records = new CSVParser(reader, format).getRecords();

        for (CSVRecord record : records) {
            String idAsString = record.values[0];
            Integer id = Integer.parseInt(idAsString);
            String first = record.values[1];
            String last = record.values[2];
            String dob = record.values[3];
            nextStudent = MockStudent.getStaticInstance(id, first, last, dob).clone();
            fileStudents.add((MockStudent) nextStudent);
        }
        uni.students.addAll(fileStudents);

    } catch (Exception ex) {
        // TODO send error message to a log file
        System.err.println("Error: " + ex.getMessage());
        importStudentsSuccessful = false;
    } finally {
        if (reader != null)
            reader.close();
    }
    return importStudentsSuccessful;
}

From source file:com.griddynamics.jagger.providers.CsvProvider.java

public Iterator<T> iterator() {

    return new AbstractIterator<T>() {

        private CSVParser parser;

        {//from ww  w  .  j  av  a2s. co  m
            init();
        }

        private void init() {
            if (path == null) {
                throw new TechnicalException("File path can't be NULL!");
            }
            try {
                parser = new CSVParser(new BufferedReader(new FileReader(new File(path))), strategy);
            } catch (FileNotFoundException e) {
                throw Throwables.propagate(e);
            }
            if (readHeader) {
                try {
                    objectCreator.setHeader(parser.getLine());
                } catch (IOException e) {
                    throw Throwables.propagate(e);
                }
            }

        }

        @Override
        protected T computeNext() {
            try {
                String[] strings = parser.getLine();
                if (strings == null) {
                    return endOfData();
                }
                return objectCreator.createObject(strings);
            } catch (IOException e) {
                throw Throwables.propagate(e);
            }
        }

    };
}