List of usage examples for org.apache.commons.csv CSVParser parse
public static CSVParser parse(final URL url, final Charset charset, final CSVFormat format) throws IOException
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 w w w.j av a2 s . com * @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:com.ibm.g11n.pipeline.example.MultiBundleCSVFilter.java
@Override public Map<String, LanguageBundle> parse(InputStream inStream, FilterOptions options) throws IOException, ResourceFilterException { Map<String, LanguageBundleBuilder> builders = new HashMap<String, LanguageBundleBuilder>(); CSVParser parser = CSVParser.parse(inStream, StandardCharsets.UTF_8, CSVFormat.RFC4180.withHeader("module", "key", "value").withSkipHeaderRecord(true)); for (CSVRecord record : parser) { String bundle = record.get(0); String key = record.get(1); String value = record.get(2); LanguageBundleBuilder bundleBuilder = builders.get(bundle); if (bundleBuilder == null) { bundleBuilder = new LanguageBundleBuilder(true); builders.put(bundle, bundleBuilder); }/*from w w w.j a v a 2 s . c o m*/ bundleBuilder.addResourceString(key, value); } Map<String, LanguageBundle> result = new TreeMap<String, LanguageBundle>(); for (Entry<String, LanguageBundleBuilder> bundleEntry : builders.entrySet()) { String bundleName = bundleEntry.getKey(); LanguageBundle bundleData = bundleEntry.getValue().build(); result.put(bundleName, bundleData); } return result; }
From source file:functions.LoadCSVdata.java
public void LoadData() { // ArrayList<Resident> residents = new ArrayList<>(); try {/*from w w w.j a v a 2 s . c o m*/ // READ FEE TABLE file = new File(PATH + FEE_FILE); System.out.println(file.toPath()); parser = CSVParser.parse(file, Charset.forName("UTF-8"), CSVFormat.DEFAULT); Fee new_fee; Resident new_res; for (CSVRecord c : parser) { // skip first record if (c.getRecordNumber() == 1) continue; System.out.println(c.get(datatype.GlobalVariable.TYPE) + ", " + c.get(datatype.GlobalVariable.AMOUNT) + ", " + c.get(datatype.GlobalVariable.PAID_BY) + ", " + c.get(datatype.GlobalVariable.PAYER)); String tmp = c.get(datatype.GlobalVariable.PAYER); //String payers; String[] payers = tmp.split(";"); System.out.println(payers.length); new_fee = new Fee(); new_fee.name = c.get(datatype.GlobalVariable.TYPE); new_fee.amount = Double.valueOf(c.get(datatype.GlobalVariable.AMOUNT)); new_fee.number_of_payer = payers.length; System.out.println("new fee: " + new_fee.name); //datatype.GlobalVariable.FEES int res_index; // add payer for (int i = 0; i < payers.length; i++) { res_index = -1; for (Resident r : datatype.GlobalVariable.RESIDENTS) { if (r.name.equals(payers[i])) { res_index = datatype.GlobalVariable.RESIDENTS.indexOf(r); break; } } System.out.println(res_index); // new resident found if (res_index == -1) { new_res = new Resident(); // System.out.println(payers[i]); new_res.name = payers[i]; datatype.GlobalVariable.RESIDENTS.add(new_res); res_index = datatype.GlobalVariable.RESIDENTS.indexOf(new_res); } // insert payer's fee // if(datatype.GlobalVariable.RESIDENTS.get(res_index).extra_fee.size()>=1) // System.out.println(datatype.GlobalVariable.RESIDENTS.get(res_index).extra_fee.get(datatype.GlobalVariable.RESIDENTS.get(res_index).extra_fee.size()-1).name); datatype.GlobalVariable.RESIDENTS.get(res_index).extra_fee.add(new_fee); // System.out.println(datatype.GlobalVariable.RESIDENTS.get(res_index).extra_fee.get(datatype.GlobalVariable.RESIDENTS.get(res_index).extra_fee.size()-1).name); } // add paid by res_index = -1; for (Resident r : datatype.GlobalVariable.RESIDENTS) { if (r.name.equals(c.get(datatype.GlobalVariable.PAID_BY))) { res_index = datatype.GlobalVariable.RESIDENTS.indexOf(r); break; } } // new resident found if (res_index == -1) { new_res = new Resident(); new_res.name = c.get(datatype.GlobalVariable.PAID_BY); datatype.GlobalVariable.RESIDENTS.add(new_res); res_index = datatype.GlobalVariable.RESIDENTS.indexOf(new_res); } // insert paid datatype.GlobalVariable.RESIDENTS.get(res_index).paid.add(new_fee); } file = new File(PATH + RESIDENTS_FILE); // READ RESIDENT TABLE parser = CSVParser.parse(file, Charset.forName("UTF-8"), CSVFormat.DEFAULT); for (CSVRecord c : parser) { // skip first record if (c.getRecordNumber() == 1) continue; System.out .println(c.get(datatype.GlobalVariable.NAME) + ", " + c.get(datatype.GlobalVariable.RENT)); int res_index = -1; for (Resident r : datatype.GlobalVariable.RESIDENTS) { if (r.name.equals(c.get(datatype.GlobalVariable.NAME))) { res_index = datatype.GlobalVariable.RESIDENTS.indexOf(r); break; } } datatype.GlobalVariable.RESIDENTS.get(res_index).basic_rent = Integer .parseInt(c.get(datatype.GlobalVariable.RENT)); } // for(int i=0;i<datatype.GlobalVariable.RESIDENTS.size();i++){ // System.out.println(datatype.GlobalVariable.RESIDENTS.get(i).name); // for(Fee f:datatype.GlobalVariable.RESIDENTS.get(i).extra_fee){ // System.out.println(f.name); // } // } } catch (Exception e) { System.out.println(e); } //return residents; }
From source file:com.ibm.watson.developer_cloud.natural_language_classifier.v1.util.TrainingDataUtils.java
/** * Creates the training data list based on a csv file. * File format needs to be UTF-8//from w ww . j a v a 2 s . co m * * @param file the CSV file * @param format the CSV format. See <a href="https://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html">available formats</a>. * @return the training data list */ public static List<TrainingData> fromCSV(final File file, CSVFormat format) { if (file == null || !file.exists()) throw new IllegalArgumentException("file must exists and be different than null"); List<TrainingData> trainingData = new ArrayList<TrainingData>(); CSVParser parser; try { parser = CSVParser.parse(file, Charset.forName("UTF-8"), format); for (CSVRecord record : parser) { if (record.size() > 1) { trainingData.add(getTrainigDataFromCSVRecord(record)); } else { log.warning(record.toString() + "skiped."); } } } catch (IOException e) { log.log(Level.SEVERE, "Error parsing the CSV", e); } return trainingData; }
From source file:com.github.r351574nc3.amex.assignment1.csv.DefaultInterpreter.java
/** * Convert records to {@link TestData}.//from w w w . j a va 2s . co m * * @return {@link Hashtable} instance which makes record lookup by name much easier. Records that belong to a given name are indexed * within the {@link Hashtable} instance. In case there is more than one instance, the object in the {@link Hashtable} is * a {@link LinkedList} which can be quickly iterated */ public Hashtable interpret(final File input) throws IOException { final CSVParser parser = CSVParser.parse(input, Charset.defaultCharset(), CSVFormat.RFC4180.withDelimiter('|')); // Using a {@link Hashtable with the name field on the CSV record as the key. A lower load factor is used to give more // priority to the time cost for looking up values. final Hashtable<String, LinkedList<TestData>> index = new Hashtable<String, LinkedList<TestData>>(2, 0.5f); for (final CSVRecord record : parser) { final EmailNotificationTestData data = toTestData(record); LinkedList<TestData> data_ls = index.get(data.getName()); if (data_ls == null) { data_ls = new LinkedList<TestData>(); index.put(data.getName(), data_ls); } data_ls.add(data); } return index; }
From source file:com.nordpos.device.csv.FileCSVInputOutput.java
@Override public void startDownloadProduct() throws DeviceInputOutputException { try {// ww w. j a v a 2 s. co m CSVParser parser = CSVParser.parse(inFile, StandardCharsets.UTF_8, format); recIterator = parser.iterator(); } catch (IOException ex) { Logger.getLogger(FileCSVInputOutput.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:br.edimarmanica.fazenda.control.LoadBBControl.java
private boolean load(Pessoa pessoa, String file) { EntityManager em = Conexao.getEntityManager(); em.getTransaction().begin();/*from w w w .j a va 2 s . c o 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:com.ibm.g11n.pipeline.example.CSVFilter.java
@Override public void merge(InputStream baseStream, OutputStream outStream, LanguageBundle languageBundle, FilterOptions options) throws IOException, ResourceFilterException { // create key-value map Map<String, String> kvMap = new HashMap<String, String>(); for (ResourceString resString : languageBundle.getResourceStrings()) { kvMap.put(resString.getKey(), resString.getValue()); }// w w w .j a va 2 s . co m CSVParser parser = CSVParser.parse(baseStream, StandardCharsets.UTF_8, CSVFormat.RFC4180.withHeader("key", "value").withSkipHeaderRecord(true)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outStream, StandardCharsets.UTF_8)); CSVPrinter printer = CSVFormat.RFC4180.withHeader("key", "value").print(writer); for (CSVRecord record : parser) { String key = record.get(0); String value = record.get(1); String trValue = kvMap.get(key); if (trValue != null) { value = trValue; } printer.printRecord(key, value); } printer.flush(); }
From source file:MasterRoomControllerFx.rooms.charts.RoomChartController.java
public void getTempData() { try {// ww w .ja v a 2 s . c om File csvData = new File("tempHistory" + 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(); i++) { if (i == 0) temp.add(Float.parseFloat(csvRecord.get(i))); if (i == 1) time.add(csvRecord.get(i)); } } } } catch (IOException ex) { Logger.getLogger(RoomChartController.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.mahisoft.elasticsearchprediction.engine.ElasticsearchGenericIndexEngine.java
private void loadData(File dataFile, Client client, String indexName, String mappingFilename) throws IOException { CSVParser parser = null;//w w w. j a va 2s. c o m PrintWriter mappingFileWriter = null; List<String> headers = new ArrayList<String>(); try { mappingFileWriter = new PrintWriter(mappingFilename, Constants.UTF8); parser = CSVParser.parse(dataFile, Charset.forName(Constants.UTF8), CSVFormat.RFC4180); for (CSVRecord csvRecord : parser) { if (csvRecord.getRecordNumber() == 1) { addHeaders(csvRecord, headers); continue; } if (csvRecord.getRecordNumber() == 2) { createIndex(client, indexName, mappingFileWriter, headers, csvRecord); } addValue(client, indexName, headers, csvRecord); } } finally { if (mappingFileWriter != null) mappingFileWriter.close(); if (parser != null) parser.close(); } LOGGER.info("Done!"); }