List of usage examples for org.apache.commons.csv CSVRecord get
public String get(final String name)
From source file:br.edimarmanica.fazenda.control.LoadBBControl.java
private boolean load(Pessoa pessoa, String file) { EntityManager em = Conexao.getEntityManager(); em.getTransaction().begin();/*from ww w. ja v a 2s . co m*/ boolean error = false; try (CSVParser parser = CSVParser.parse(new File(file), Charset.forName("ISO-8859-1"), CSVFormat.EXCEL.withHeader())) { for (CSVRecord record : parser) { TipoCaixa tc = new TipoCaixa(); tc.setCdBb(new BigInteger(record.get("Nmero do documento"))); if (tc.getCdBb() != null) { List<TipoCaixa> tcs = daoTipo.search(tc); if (tcs != null && tcs.size() == 1) { Caixa caixa = new Caixa(); caixa.setCdTipoCaixa(tcs.get(0)); caixa.setCdPessoa(pessoa); caixa.setVlCaixa((new BigDecimal(record.get("Valor"))).abs()); caixa.setDtPagamento(new Date(record.get("Data"))); caixa.setDtVencimento(new Date(record.get("Data"))); caixa.setDsCaixa("IMPORTAO BB 02"); em.persist(caixa); } } } } catch (IOException ex) { Logger.getLogger(LoadBBControl.class.getName()).log(Level.SEVERE, null, ex); error = true; } if (!error) { em.getTransaction().commit(); } else { em.getTransaction().rollback(); } em.close(); return !error; }
From source file:eu.verdelhan.ta4j.indicators.trackers.ParabolicSarIndicatorTest.java
@Test public void shouldCalculateSARMatchingKnownFTSE100ExampleFromSpreadsheet() 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 .co 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-Spreadsheet-2017-04-27.csv"); int i = 0; for (CSVRecord expectedSAR : expectedSARsFromFile) { if (i > 3) { //ignore 1st four records as they are zero 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++; } }
From source file:ColdestWeather.ColdestWeather.java
public void testColdestHourInFile() { FileResource fr = new FileResource(); CSVRecord coldest = coldestHourInFile(fr.getCSVParser()); System.out.println(/* w ww . j a v a2s. c o m*/ "Coldest temperature was: " + coldest.get("TemperatureF") + " at " + coldest.get("DateUTC")); }
From source file:com.awesheet.managers.CSVManager.java
/** * Imports a Sheet from a CSV file in the specified path. * @param path a CSV File Path./*from ww w . j a v a2 s .c o m*/ * @return a new Sheet or null if parsing failed */ public Sheet importSheet(String path) { File csvData = new File(path); // Parse the CSV file. CSVParser parser; try { parser = CSVParser.parse(csvData, Charset.defaultCharset(), CSVFormat.RFC4180); } catch (IOException e) { return null; } // Create our new sheet. Sheet sheet = new Sheet("Imported Sheet"); // Populate its cells. for (CSVRecord record : parser) { for (int x = 0; x < record.size(); ++x) { sheet.setCellValue(x, (int) record.getRecordNumber() - 1, record.get(x), true); } } return sheet; }
From source file:de.speexx.jira.jan.service.issue.IssueCoreFieldConfigLoader.java
IssueCoreFieldDescriptionEntry createIssueFieldConfigEntry(final CSVRecord record, final ClassLoader cl) { assert record != null; assert cl != null; try {/*w ww. j a v a2 s. c o m*/ final String fieldName = record.get(FIELDNAME_HEADER).trim(); final Class<?> valueFetcherType = fetchValueFetcherType(record, cl); final Method fieldGetter = createFieldGetterMethod(record); final boolean ignore = fetchIgnore(record); final Set<FieldName> aliases = fetchAliasses(record); return new IssueCoreFieldDescriptionEntry(this.fieldNameService.createFieldName(fieldName), aliases, fieldGetter, ignore, valueFetcherType); } catch (final ClassNotFoundException | NoSuchMethodException e) { throw new JiraAnalyzeException(e); } }
From source file:com.github.hexosse.memworth.WorthBuilder.java
/** * Parse the csv file//from w ww . j ava2 s. c om * * @return WorthBuilder */ public WorthBuilder parse() { try { // Free list of items items.clear(); // Load csv file into file reader FileReader reader = new FileReader(csvFile); // parse csv file using specified file format for (CSVRecord record : csvFormat.parse(reader)) { // Skip empty lines if (record.get(0).isEmpty()) continue; // Skip title if (record.get(0).equals("ID")) continue; // Skip item without price if (record.get(3).isEmpty()) continue; if (record.get(4).isEmpty()) continue; // Create Item String[] idData = record.get(0).replaceAll("[^0-9]", " ").trim().split(" "); int id = Integer.parseInt(idData[0]); int data = Integer.parseInt(idData.length > 1 ? idData[1] : "0"); String comment = record.get(1); String sworth = record.get(4).trim().replaceAll(" ", "") .replaceAll("^[^a-zA-Z0-9\\s]+|[^a-zA-Z0-9\\s]+$", ""); Number worth = numFormat.parse(sworth); // Add Item to list if (worth.doubleValue() != 0) items.add(new Item(id, data, comment, worth.doubleValue())); } // Close csv file reader.close(); // Sort the item list Collections.sort(items); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } return this; }
From source file:MasterRoomControllerFx.rooms.charts.RoomChartController.java
public void getHumData() { try {/*ww w .j ava2s. c om*/ File csvData = new File("humHistory" + roomRow + roomColumn + ".csv"); if (csvData.exists()) { CSVParser parser = CSVParser.parse(csvData, StandardCharsets.UTF_8, CSVFormat.EXCEL.withDelimiter(';')); for (CSVRecord csvRecord : parser) { for (int i = 0; i < csvRecord.size() - 1; i++) { hum.add(Float.parseFloat(csvRecord.get(i))); } } } } catch (IOException ex) { Logger.getLogger(RoomChartController.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:eu.fthevenet.binjr.data.codec.CsvDecoder.java
/** * Decodes data from the provided stream and invoke the provided {@link Consumer} for each decoded record. * * @param in the {@link InputStream} for the CSV file * @param headers a list of the headers to keep from decoded records * @param mapToResult the function to invoke for reach decoded record * @throws IOException in the event of an I/O error. * @throws DecodingDataFromAdapterException if an error occurred while decoding the CSV file. *//* w ww . j a v a2s.c om*/ public void decode(InputStream in, List<String> headers, Consumer<DataSample<T>> mapToResult) throws IOException, DecodingDataFromAdapterException { try (Profiler ignored = Profiler.start("Building time series from csv data", logger::trace)) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(in, encoding))) { CSVFormat csvFormat = CSVFormat.DEFAULT.withAllowMissingColumnNames(false).withFirstRecordAsHeader() .withSkipHeaderRecord().withDelimiter(delimiter); Iterable<CSVRecord> records = csvFormat.parse(reader); for (CSVRecord csvRecord : records) { ZonedDateTime timeStamp = dateParser.apply(csvRecord.get(0)); DataSample<T> tRecord = new DataSample<>(timeStamp); for (String h : headers) { tRecord.getCells().put(h, numberParser.apply(csvRecord.get(h))); } mapToResult.accept(tRecord); } } } }
From source file:com.cotrino.langnet.GenerateVisualization.java
private void getLanguages(String file) throws IOException { amountWordsPerLanguage = new HashMap<String, Integer>(); Reader reader = new FileReader(file); CSVParser parser = new CSVParser(reader, csvFormat); for (CSVRecord record : parser) { String language = record.get("Language"); int words = Integer.parseInt(record.get("Words")); amountWordsPerLanguage.put(language, words); }/* w w w . j a v a2s . co m*/ parser.close(); reader.close(); }
From source file:de.speexx.jira.jan.service.issue.IssueCoreFieldConfigLoader.java
Class<?> fetchValueFetcherType(final CSVRecord record, final ClassLoader cl) throws ClassNotFoundException { assert record != null; assert cl != null; final String valueFetcherTypeName = record.get(VALUE_FETCHER_TYPE_HEADER); return Class.forName(valueFetcherTypeName, false, cl); }