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:formats.db.fileformats.csv.RunnerCsv.java

public static void main(String[] args) throws IOException {

    CSVFormat format = CSVFormat.RFC4180.withHeader().withDelimiter(',');

    //initialize the CSVParser object

    CSVParser parser = new CSVParser(new FileReader("D:\\customers.csv"), format);

    List<Customers> customersList = new ArrayList<Customers>();
    for (CSVRecord record : parser) {
        Customers customers = new Customers();

        customers.setId_cs(Integer.parseInt(record.get("id_cs")));
        customers.setF_name(record.get("f_name"));
        customers.setL_name(record.get("l_name"));
        customers.setDiscount(Integer.parseInt(record.get("discount")));
        customers.setLicense(record.get("license"));

        customersList.add(customers);//  w  ww .  j  ava2  s.  c  om
    }
    //close the parser
    parser.close();

    System.out.println(customersList);
}

From source file:com.blockhaus2000.csvviewer.CsvViewerMain.java

public static void main(final String[] args) {
    try (final CSVParser parser = new CSVParser(new InputStreamReader(System.in), CSVFormat.DEFAULT)) {
        final Table table = new Table();

        final Map<String, Integer> headerMap = parser.getHeaderMap();
        if (headerMap != null) {
            final TableRow headerRow = new TableRow();
            headerMap.keySet().forEach(headerData -> headerRow.addCell(new TableRowCell(headerData)));
            table.setHeaderRow(headerRow);
        }/*from   w w w  .j  a  v a  2 s .c  om*/

        final AtomicBoolean asHeader = new AtomicBoolean(headerMap == null);
        parser.getRecords().forEach(record -> {
            final TableRow row = new TableRow();
            record.forEach(rowData -> row.addCell(new TableRowCell(rowData)));
            if (asHeader.getAndSet(false)) {
                table.setHeaderRow(row);
            } else {
                table.addRow(row);
            }
        });

        System.out.println(table.getFormattedString());
    } catch (final IOException cause) {
        throw new RuntimeException("An error occurred whilst parsing stdin!", cause);
    }
}

From source file:com.chargebee.CSV.PhoneBook.PhoneBook2.PhoneBook.java

public static void main(String[] args) throws IOException, Exception {
    String source = System.getProperty("user.home") + "/input2.csv";

    PhoneBook pb = new PhoneBook();

    CSVParser parser = new CSVParser(new FileReader(source), CSVFormat.EXCEL.withHeader());

    pb.directory(parser);/*from   w  w w  . j a va2 s.  c om*/
    parser.close();

}

From source file:com.chargebee.Application.MappingHeaders.java

public static void main(String[] args) throws Exception {
    String source1 = System.getProperty("user.home") + "/Output-3.csv"; // Source CSV file containing the customer details 
    String source2 = System.getProperty("user.home") + "/header.json";//Json file containing the customer id and token
    String output = System.getProperty("user.home") + "/Output-4.csv";// The destination CSV file.
    //        Scanner sc = new Scanner(System.in);
    //        System.out.println("Input CSV File: ");
    //        String source1 = sc.nextLine();
    ///*from   w w  w. jav a 2  s . com*/
    //        System.out.println("config File: ");
    //        String source2 = sc.nextLine();
    //
    //        System.out.println("Output CSV File: ");
    //        String output = sc.nextLine();
    //        

    MappingHeaders objHm = new MappingHeaders();
    JSONObject jobj = objHm.readJsonData(source2);
    CSVPrinter printer = new CSVPrinter(new FileWriter(output),
            CSVFormat.EXCEL.withRecordSeparator("\n").withDelimiter(','));
    CSVParser parser = new CSVParser(new FileReader(source1), CSVFormat.EXCEL.withHeader());
    objHm.extractJsonData(jobj, printer, parser);
    parser.close();
    printer.close();

}

From source file:com.chargebee.JDBC.PhoneBook.Phnbk.java

public static void main(String[] args) throws IOException, Exception {
    String source = System.getProperty("user.home") + "/input.csv";
    com.chargebee.JDBC.PhoneBook.Phnbk pb = new com.chargebee.JDBC.PhoneBook.Phnbk();
    CSVParser parser = new CSVParser(new FileReader(source), CSVFormat.EXCEL.withHeader());
    pb.directory(parser);/*from   w ww  . j  a  va2  s  .c  om*/
    parser.close();

}

From source file:Phnbk.java

public static void main(String[] args) throws IOException, Exception {
    String source = System.getProperty("user.home") + "/input.csv";
    Phnbk pb = new Phnbk();
    CSVParser parser = new CSVParser(new FileReader(source), CSVFormat.EXCEL.withHeader());
    pb.directory(parser);/* ww  w  .  j av  a 2 s.c  o m*/
    parser.close();

}

From source file:br.edimarmanica.weir2.rule.Loader.java

/**
 *
 * @param rule/*from  ww  w  . j  a va  2s .co m*/
 * @param formatted valores formatados como na avaliao?
 * @return Map<PageId, Value>
 *
 */
public static Map<String, String> loadPageValues(File rule, boolean formatted) {
    Map<String, String> pageValues = new HashMap<>();

    try (Reader in = new FileReader(rule.getAbsolutePath())) {
        try (CSVParser parser = new CSVParser(in, CSVFormat.EXCEL.withHeader())) {
            for (CSVRecord record : parser) { //para cada value
                String url = formatURL(record.get("URL"));
                String value = record.get("EXTRACTED VALUE");

                if (formatted) {
                    value = Formatter.formatValue(value);
                }

                if (!value.trim().isEmpty()) {
                    pageValues.put(url, value);
                }
            }
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Loader.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Loader.class.getName()).log(Level.SEVERE, null, ex);
    }
    return pageValues;
}

From source file:ca.craigthomas.visualclassifier.dataset.DataSetReader.java

/**
 * Read from a CSV file, and return the samples as a list of doubles.
 * /*from w  ww .  j  a  v  a  2 s.  c  o m*/
 * @param filename the name of the file to read from
 * @return the list of samples from the file
 * @throws IOException
 */
public static List<List<Double>> readCSVFile(String filename) throws IOException {
    File file = new File(filename);
    String fileContents = FileUtils.readFileToString(file);
    Reader reader = new StringReader(fileContents);
    CSVFormat format = CSVFormat.EXCEL;
    CSVParser parser = new CSVParser(reader, format);

    List<CSVRecord> records = parser.getRecords();
    List<List<Double>> inputs = new ArrayList<List<Double>>();

    for (CSVRecord record : records) {
        List<Double> inputLine = new ArrayList<Double>();
        for (int index = 0; index < record.size(); index++) {
            String value = record.get(index);
            inputLine.add(Double.parseDouble(value));
        }
        inputs.add(inputLine);
    }
    parser.close();
    return inputs;
}

From source file:com.chargebee.MethodBank.MethodBank.java

public static CSVParser parserInitializer(String csvInput) throws IOException, Exception {
    CSVParser parser = new CSVParser(new FileReader(csvInput), CSVFormat.EXCEL);
    return parser;
}

From source file:de.tudarmstadt.ukp.dkpro.argumentation.sequence.report.ConfusionMatrixTools.java

public static ConfusionMatrix tokenLevelPredictionsToConfusionMatrix(File predictionsFile) throws IOException {
    ConfusionMatrix cm = new ConfusionMatrix();

    CSVParser csvParser = new CSVParser(new FileReader(predictionsFile),
            CSVFormat.DEFAULT.withCommentMarker('#'));

    for (CSVRecord csvRecord : csvParser) {
        // update confusion matrix
        cm.increaseValue(csvRecord.get(0), csvRecord.get(1));
    }/*  w w w .  jav  a 2  s  .  c o  m*/

    return cm;
}