Example usage for org.apache.commons.csv CSVRecord get

List of usage examples for org.apache.commons.csv CSVRecord get

Introduction

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

Prototype

public String get(final String name) 

Source Link

Document

Returns a value by name.

Usage

From source file:net.sourceforge.ganttproject.io.CsvImportTest.java

public void testBasic() throws Exception {
    String header = "A, B";
    String data = "a1, b1";
    final AtomicBoolean wasCalled = new AtomicBoolean(false);
    GanttCSVOpen.RecordGroup recordGroup = new GanttCSVOpen.RecordGroup("AB",
            ImmutableSet.<String>of("A", "B")) {
        @Override//  w  w w .  j  a  v a 2 s.  c  o m
        protected boolean doProcess(CSVRecord record) {
            wasCalled.set(true);
            assertEquals("a1", record.get("A"));
            assertEquals("b1", record.get("B"));
            return true;
        }
    };
    GanttCSVOpen importer = new GanttCSVOpen(createSupplier(Joiner.on('\n').join(header, data)), recordGroup);
    importer.load();
    assertTrue(wasCalled.get());

    // Now test with one empty line between header and data
    wasCalled.set(false);
    importer = new GanttCSVOpen(createSupplier(Joiner.on('\n').join(header, "", data)), recordGroup);
    importer.load();
    assertTrue(wasCalled.get());
}

From source file:biz.itcons.wsdm.hw2_2.NaiveBayes.java

/**
 * Checks feasibility of classification based on test data. For given set
 * it runs NaiveBayes classifier and checks the result with one that is
 * provided for test data./*from  w  w  w .j  av  a  2 s. com*/
 * @param file A CSV file with documents that are classified.
 * @param classifierPos a position in CSV file where classification is
 * stored (zero based indexing)
 * @param documentPos a position in CSV file where document body is stored
 * (zero based indexing)
 * @return ration of correctly classified documents vs. all test documents.
 * @throws FileNotFoundException
 * @throws IOException 
 */
public double testDataSet(String file, int classifierPos, int documentPos)
        throws FileNotFoundException, IOException {
    int testSetCount = 0;
    int correctClass = 0;
    try (Reader in = new FileReader(file)) {
        LOGGER.debug("Opening training data set: " + file);
        for (CSVRecord record : CSVFormat.EXCEL.parse(in)) {
            testSetCount++;
            String classification = classifyDocument(record.get(documentPos));

            if (record.get(classifierPos).equals(classification)) {
                correctClass++;
            }
        }
        LOGGER.trace("Read " + testSetCount + " from test set");
        LOGGER.info("success ratio: " + correctClass + " of " + testSetCount);
        return (double) correctClass / testSetCount;
    }
}

From source file:com.github.jferard.pgloaderutils.sniffer.csd.CSDSchemaPattern.java

private void addFields(CSDFieldFactory<F> factory, List<F> newFields, F field, CSVRecord firstRecord,
        int begin) {
    if (firstRecord == null)
        return;/*  w  ww.j  ava  2 s.c o m*/

    for (int i = begin; i < firstRecord.size(); i++) {
        String name = firstRecord.get(i);
        newFields.add(factory.create(field.getType(), name, name, true));
    }
}

From source file:edu.uiowa.icts.bluebutton.dao.LabTestHome.java

@Override
public void importCSV(InputStream fileInputStream) throws IOException {
    Reader in = new BufferedReader(new InputStreamReader(fileInputStream));
    Iterable<CSVRecord> records = CSVFormat.EXCEL.withHeader("LAB_TEST_ID", "LAB_TEST_NAME", "TEST_DESCRIPTION",
            "UNITS", "LOINC_NUM", "DATETIME_CREATED", "DATETIME_LASTMODIFIED").withSkipHeaderRecord(true)
            .parse(in);/*from   w  w w  .  j av a 2 s . c o  m*/
    for (CSVRecord record : records) {
        LabTest labTest = new LabTest();
        labTest.setLabTestId(new Integer(record.get("LAB_TEST_ID")));
        labTest.setName(record.get("LAB_TEST_NAME"));
        labTest.setDescription(record.get("TEST_DESCRIPTION"));
        labTest.setUnits(record.get("UNITS"));
        labTest.setLoincCode(record.get("LOINC_NUM"));
        labTest.setDateCreated(record.get("DATETIME_CREATED"));
        labTest.setDateUpdated(record.get("DATETIME_LASTMODIFIED"));

        this.sessionFactory.getCurrentSession().save(labTest);
    }

}

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

private void parseCalendarFile(final Reader calendarReader, final Multimap<LocalDate, String> serviceTypesMap)
        throws IOException {

    final CSVParser calendarParser = new CSVParser(calendarReader, CSVFormat.DEFAULT.withHeader());
    final List<CSVRecord> calendarRecords = calendarParser.getRecords();

    LocalDate earliestDate = null;
    LocalDate latestDate = null;//from  w w  w  .ja  v  a 2  s. c om
    for (final CSVRecord record : calendarRecords) {
        final String serviceType = record.get("service_id");

        final LocalDate start = LocalDate.parse(record.get("start_date"), DateTimeFormatter.BASIC_ISO_DATE);
        if (earliestDate == null || start.isBefore(earliestDate)) {
            earliestDate = start;
        }

        final LocalDate end = LocalDate.parse(record.get("end_date"), DateTimeFormatter.BASIC_ISO_DATE);
        if (latestDate == null || end.isAfter(latestDate)) {
            latestDate = end;
        }

        final EnumSet<DayOfWeek> daysOfWeek = EnumSet.noneOf(DayOfWeek.class);
        if (record.get("monday").equals("1")) {
            daysOfWeek.add(DayOfWeek.MONDAY);
        }
        if (record.get("tuesday").equals("1")) {
            daysOfWeek.add(DayOfWeek.TUESDAY);
        }
        if (record.get("wednesday").equals("1")) {
            daysOfWeek.add(DayOfWeek.WEDNESDAY);
        }
        if (record.get("thursday").equals("1")) {
            daysOfWeek.add(DayOfWeek.THURSDAY);
        }
        if (record.get("friday").equals("1")) {
            daysOfWeek.add(DayOfWeek.FRIDAY);
        }
        if (record.get("saturday").equals("1")) {
            daysOfWeek.add(DayOfWeek.SATURDAY);
        }
        if (record.get("sunday").equals("1")) {
            daysOfWeek.add(DayOfWeek.SUNDAY);
        }

        LocalDate targetDate = start;
        while (!targetDate.isAfter(end)) {
            if (daysOfWeek.contains(targetDate.getDayOfWeek())) {
                serviceTypesMap.put(targetDate, serviceType);
            }
            targetDate = targetDate.plusDays(1);
        }
    }
}

From source file:GUI.ReadFile.java

public boolean readTrace(String fileName) {
    FileReader fileReader;/*from  www .java2s  .  c om*/
    CSVParser csvFileParser;
    boolean isSuccess = true;
    CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(TRACE_HEADER_MAPPING);

    try {
        ArrayList<String> Activity_set = new ArrayList<String>();
        HashSet<String> ID_set = new HashSet<String>();
        traces = new ArrayList<Trace>();
        //initialize FileReader object
        System.out.println(fileName);
        fileReader = new FileReader(fileName);

        //initialize CSVParser object
        csvFileParser = new CSVParser(fileReader, csvFileFormat);
        //Get a list of CSV file records
        List<CSVRecord> csvRecords = csvFileParser.getRecords();
        Trace t = new Trace("");
        //Read the CSV file records starting from the second record to skip the header
        for (int i = 1; i < csvRecords.size(); i++) {
            CSVRecord record = csvRecords.get(i);
            String ID = record.get(CaseID);
            if (!ID_set.contains(ID) || (i == csvRecords.size() - 1)) {
                //Discard void trace
                if (i != 1) {
                    traces.add(t);
                }
                ID_set.add(ID);
                t = new Trace(ID);
            }
            Activity ac = new Activity(record.get(Activity), record.get(StartTime), record.get(CompleteTime),
                    record.get(Timestamp));
            t.add_activity(ac);

            if (!Activity_set.contains(ac.get_name())) {
                Activity_set.add(ac.get_name());
            }
        }
        //sort activity set by string
        Collections.sort(Activity_set);

        //sort trace by ID
        Collections.sort(traces, new Comparator<Trace>() {
            @Override
            public int compare(Trace t1, Trace t2) {
                return Integer.parseInt(t1.get_ID()) < Integer.parseInt(t2.get_ID()) ? -1 : 1;
            }
        });
        //Set activity set for each trace
        for (Trace T : traces) {
            T.set_ActivitySet((List<String>) Activity_set.clone());
        }

    } catch (Exception e) {
        System.out.println("Error in CsvFileReader !!!");
        e.printStackTrace();
        isSuccess = false;
        return isSuccess;
    }
    if (isSuccess) {
        try {
            fileReader.close();
            csvFileParser.close();
        } catch (IOException e) {
            System.out.println("Error while closing fileReader/csvFileParser !!!");
        }
    }
    return isSuccess;
}

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

private void parseFrequenciesFile(final ImmutableMultimap.Builder<String, FrequencyRecord> builder,
        final Reader frequenciesReader) throws FileNotFoundException, IOException {

    final CSVParser frequenciesParser = new CSVParser(frequenciesReader, CSVFormat.DEFAULT.withHeader());
    final List<CSVRecord> frequenciesRecords = frequenciesParser.getRecords();

    for (CSVRecord record : frequenciesRecords) {
        final String tripId = record.get("trip_id");

        final FrequencyRecord frequencyRecord = new FrequencyRecord(tripId,
                TransitTime.parse(record.get("start_time")), TransitTime.parse(record.get("end_time")),
                Duration.ofSeconds(Long.parseLong(record.get("headway_secs"))));
        builder.put(tripId, frequencyRecord);
    }//from  w w w . j a  va  2s. c o m
}

From source file:ColdestWeather.ColdestWeather.java

public void testFileWithColdestTemperature() {
    String ColdestTempFile = fileWithColdestTemperature();
    //Path needs to absolute but only the filename is printed.
    System.out.println(/*from ww w  . j  a  va 2  s  .  c  o  m*/
            "Coldest day was in the file " + ColdestTempFile.substring(ColdestTempFile.lastIndexOf('/') + 1));
    FileResource fr = new FileResource(ColdestTempFile);
    CSVParser parser = fr.getCSVParser();
    CSVRecord coldest = coldestHourInFile(parser);
    System.out.println("Coldest temperature on that day was " + coldest.get("TemperatureF"));
    System.out.println("All the Temperatures on the coldest day were:");
    FileResource fr2 = new FileResource(ColdestTempFile);
    CSVParser parser2 = fr2.getCSVParser();
    for (CSVRecord row : parser2) {
        System.out.println(row.get("DateUTC") + ": " + row.get("TemperatureF"));
    }
}

From source file:convertCSV.ConvertCSV.java

/**
 * @param fichier_/*from  www  .j  a  v  a 2s  .c om*/
 * @throws java.io.IOException
 */
public void importer(String fichier_) throws IOException {
    // TODO code application logic here

    float lat, lon, ele, secjour;
    int bpm;
    List<MonPoint> points = new ArrayList<>();
    Reader in = null;
    CSVParser parser;
    List<CSVRecord> list;
    GPXWriter monGPX = new GPXWriter();

    // lecture du CSV
    try {

        System.out.println("Lecture de " + fichier_);
        in = new FileReader(fichier_);

    } catch (FileNotFoundException ex) {
        Logger.getLogger(ConvertCSV.class.getName()).log(Level.SEVERE, null, ex);
    }

    parser = new CSVParser(in, CSVFormat.EXCEL);
    list = parser.getRecords();
    list.remove(0);

    // remplissage de la liste de point GPX
    if (in != null) {
        for (CSVRecord elem : list) {

            try {

                // on recupere les donnees dans le CSV
                lat = Float.parseFloat(elem.get(0));
                lon = Float.parseFloat(elem.get(1));
                ele = Float.parseFloat(elem.get(2));
                secjour = Float.parseFloat(elem.get(3));
                if (elem.size() > 4) {
                    bpm = Integer.parseInt(elem.get(4));
                    points.add(new MonPoint(lat, lon, ele, secjour, bpm));
                } else {
                    points.add(new MonPoint(lat, lon, ele, secjour));
                }

            } catch (NumberFormatException ex) {
                System.out.println(elem.toString());
            }

        }

        // ecriture du GPX
        monGPX.writePath("C:\\Users\\vincent\\Desktop\\today.gpx", "Training", points);
        in.close();

    }

    System.exit(0);
}

From source file:eu.verdelhan.ta4j.indicators.trackers.ParabolicSarIndicatorTest.java

@Test
public void shouldCalculateSARMatchingKnownFTSE100ExampleFromTradingCompany() throws IOException {
    Iterable<CSVRecord> pricesFromFile = getCsvRecords("/FTSE100MiniOneMinutePrices-2017-04-27.csv");

    List<Tick> ticks = new ArrayList<Tick>();
    for (CSVRecord price : pricesFromFile) {
        ticks.add(new MockTick(new Double(price.get(5)), new Double(price.get(11)), new Double(price.get(9)),
                new Double(price.get(7))));
    }// ww w.  ja  va  2 s  .  c o m

    //Price CSV field mappings
    //5 = opening bid, 7 = lowest bid, 9 = highest bid, 11 = closing bid

    ParabolicSarIndicator sar = new ParabolicSarIndicator(new MockTimeSeries(ticks), 1);

    Iterable<CSVRecord> expectedSARsFromFile = getCsvRecords(
            "/FTSE100MiniOneMinute-Expected-SARs-From-IGIndex-2017-04-27.csv");

    int i = 0;
    for (CSVRecord expectedSAR : expectedSARsFromFile) {
        final Double expected = new Double(expectedSAR.get(2));
        final Decimal actual = sar.getValue(i);
        System.out.println("Expected=" + expected + " Actual=" + actual);

        assertDecimalEquals(actual, expected);
        i++;
    }

}