Example usage for org.apache.commons.csv CSVFormat EXCEL

List of usage examples for org.apache.commons.csv CSVFormat EXCEL

Introduction

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

Prototype

CSVFormat EXCEL

To view the source code for org.apache.commons.csv CSVFormat EXCEL.

Click Source Link

Document

Excel file format (using a comma as the value delimiter).

Usage

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

/**
 *
 * @param rule/*from  w w w.  j  a v a 2 s .c o  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:com.lithium.flow.util.CsvFormats.java

@Nonnull
public static CSVFormat fromConfig(@Nonnull Config config) {
    checkNotNull(config);//  ww w .  ja v  a2 s.com
    switch (config.getString("csv.format", "default")) {
    case "default":
        return CSVFormat.DEFAULT;
    case "excel":
        return CSVFormat.EXCEL;
    case "mysql":
        return CSVFormat.MYSQL;
    case "rfc4180":
        return CSVFormat.RFC4180;
    case "tdf":
        return CSVFormat.TDF;
    case "custom":
        return CSVFormat.newFormat(getChar(config, "csv.delimiter", ','))
                .withAllowMissingColumnNames(getBoolean(config, "csv.allowMissingColumnNames"))
                .withCommentMarker(getChar(config, "csv.commentMarker"))
                .withEscape(getChar(config, "csv.escape")).withHeader(getHeader(config, "csv.header"))
                .withIgnoreEmptyLines(getBoolean(config, "csv.ignoreEmptyLines"))
                .withIgnoreSurroundingSpaces(getBoolean(config, "csv.ignoreSurroundingSpaces"))
                .withNullString(getString(config, "csv.nullString")).withQuote(getChar(config, "csv.quote"))
                .withQuoteMode(getQuoteMode(config, "csv.quoteMode"))
                .withRecordSeparator(getString(config, "csv.recordSeparator"))
                .withSkipHeaderRecord(getBoolean(config, "csv.skipHeaderRecord"));
    default:
        return CSVFormat.DEFAULT;
    }
}

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:main.StratioENEI.java

private static void readFromCSV(String filename, float[][] outCustos, List<String> outCidades)
        throws IOException {
    CSVParser c = new CSVParser(new FileReader(filename),
            CSVFormat.EXCEL.withDelimiter(';').withNullString(""));
    int lineNumber;
    for (CSVRecord record : c) {
        // System.out.println(record);

        if ((lineNumber = (int) record.getRecordNumber()) == 1) {
            continue;
        }//from  w  w  w . j av  a  2 s .com
        outCidades.add(record.get(0));
        //     System.out.printf("\n%10s", record.get(0));
        for (int i = lineNumber - 1; i < outCustos.length + 1; i++) {
            //        System.out.printf("\t%-6s|", (record.get(i) == null) ? "null" : record.get(i));
            outCustos[lineNumber - 2][i - 1] = outCustos[i - 1][lineNumber - 2] = Float
                    .parseFloat((record.get(i) == null) ? "0.0" : record.get(i));
        }
    }
}

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

public static CSVPrinter printerInitializer(String csvOut) throws IOException, Exception {
    CSVPrinter printer = new CSVPrinter(new FileWriter(csvOut),
            CSVFormat.EXCEL.withRecordSeparator("\n").withDelimiter(','));
    return printer;
}

From source file:br.edimarmanica.weir2.rule.type.RulesDataTypeController.java

/**
 * Persiste the datatype of each rule//  ww w. j  av  a 2  s. co  m
 *
 * @param site
 */
public static void persiste(Site site) {
    Map<String, DataType> ruleType = new HashMap<>();

    File dirInput = new File(Paths.PATH_INTRASITE + "/" + site.getPath() + "/extracted_values");
    for (File rule : dirInput.listFiles()) {
        ruleType.put(rule.getName(), RuleDataType.getMostFrequentType(rule));
    }

    File dirOutput = new File(Paths.PATH_WEIR_V2 + "/" + site.getPath());
    dirOutput.mkdirs();

    File file = new File(dirOutput.getAbsolutePath() + "/types.csv");
    String[] HEADER = { "RULE", "TYPE" };
    CSVFormat format = CSVFormat.EXCEL.withHeader(HEADER);

    try (Writer out = new FileWriter(file)) {
        try (CSVPrinter csvFilePrinter = new CSVPrinter(out, format)) {
            for (String rule : ruleType.keySet()) {
                List<String> dataRecord = new ArrayList<>();
                dataRecord.add(rule);
                dataRecord.add(ruleType.get(rule).name());
                csvFilePrinter.printRecord(dataRecord);
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(RulesDataTypeController.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:algoritma.LoadData.java

public void read(String filePath, int confi) throws FileNotFoundException, IOException {
    FileReader fileReader = null;
    CSVParser csvFileParser = null;/*from  w  w  w  .  j a  v  a2  s .  com*/
    CSVFormat csvFileFormat = CSVFormat.EXCEL.withFirstRecordAsHeader();
    fileReader = new FileReader(filePath);
    csvFileParser = new CSVParser(fileReader, csvFileFormat);
    List csvRecords = csvFileParser.getRecords();

    for (int i = 0; i < csvRecords.size(); i++) {
        CSVRecord record = (CSVRecord) csvRecords.get(i);
        double confidence = Double.parseDouble(record.get("confidence"));
        if (confidence >= confi) {
            Point p = new Point();
            p.setBrightness(Double.parseDouble(record.get("brightness")));
            p.setBright_t31(Double.parseDouble(record.get("bright_t31")));
            p.setFrp(Double.parseDouble(record.get("frp")));
            p.add(p);
        }
    }
}

From source file:com.ibm.watson.developer_cloud.natural_language_classifier.v1.util.TrainingDataUtils.java

/**
 * Converts a training like argument list to a CSV representation.
 * //from w w  w . ja va2  s. c  om
 * @param data
 *            the training data data
 * @return the string with the CSV representation for the training data
 */
public static String toCSV(final TrainingData... data) {
    Validate.notEmpty(data, "data cannot be null or empty");

    StringWriter stringWriter = new StringWriter();
    try {
        CSVPrinter printer = new CSVPrinter(stringWriter, CSVFormat.EXCEL);
        for (TrainingData trainingData : data) {
            if (trainingData.getText() == null || trainingData.getClasses() == null
                    || trainingData.getClasses().isEmpty())
                log.log(Level.WARNING, trainingData + " couldn't be converted to a csv record");
            else {
                List<String> record = new ArrayList<String>();
                record.add(trainingData.getText());
                record.addAll(trainingData.getClasses());
                printer.printRecord(record.toArray());
            }
        }
        printer.close();
    } catch (IOException e) {
        log.log(Level.SEVERE, "Error creating the csv", e);
    }

    return stringWriter.toString();
}

From source file:com.vsthost.rnd.commons.math.ext.linear.IOUtils.java

/**
 * Reads a matrix of double values from the reader provided.
 *
 * @param reader The reader which the values to be read from.
 * @return A matrix//from  w ww  . ja va  2 s .c  om
 * @throws IOException As thrown by the CSV parser.
 */
public static RealMatrix readMatrix(Reader reader) throws IOException {
    // Initialize the return value:
    List<double[]> retval = new ArrayList<>();

    // Parse and get the iterarable:
    Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(reader);

    // Iterate over the records and populate return value:
    for (CSVRecord record : records) {
        double[] row = new double[record.size()];
        for (int i = 0; i < record.size(); i++) {
            row[i] = Double.parseDouble(record.get(i));
        }
        retval.add(row);
    }

    // Convert the list to an array:
    double[][] retvalArray = new double[retval.size()][];
    retval.toArray(retvalArray);

    // Done, return the array:
    return MatrixUtils.createRealMatrix(retvalArray);
}

From source file:edu.ucla.cs.scai.swim.qa.ontology.dbpedia.tipicality.DbpediaCategoryAttributeCounts.java

private static void processFile(File csvData, String category) throws IOException {
    BufferedReader in = new BufferedReader(
            new InputStreamReader(new GZIPInputStream(new FileInputStream(csvData))));
    //CSVParser parser = CSVParser.parse(csvData, Charset.defaultCharset(), CSVFormat.RFC4180);
    CSVParser parser = CSVFormat.EXCEL.parse(in);
    int r = 0;/*from w w  w.jav  a  2 s.  c  om*/
    ArrayList<Integer> attributePositions = new ArrayList<>();
    ArrayList<String> attributeNames = new ArrayList<>();
    HashMap<String, Integer> thisCategoryAttributeCounts = new HashMap<>();
    categoryAttributeCount.put(category, thisCategoryAttributeCounts);
    for (CSVRecord csvRecord : parser) {
        if (r == 0) {
            Iterator<String> it = csvRecord.iterator();
            it.next(); //skip URI
            if (!it.hasNext()) { //it is an empty file
                return;
            }
            it.next(); //skip rdf-schema#label
            it.next(); //skip rdf-schema#comment
            int c = 2;
            for (; it.hasNext();) {
                c++;
                String attr = it.next();
                if (!attr.endsWith("_label")) {
                    attributePositions.add(c);
                }
            }
            categories.add(category);
        } else if (r == 1) {
            Iterator<String> it = csvRecord.iterator();
            it.next(); //skip uri
            it.next(); //skip rdf-schema#label
            it.next(); //skip rdf-schema#comment
            int c = 2;
            int i = 0;
            while (i < attributePositions.size()) {
                c++;
                String attr = it.next();
                if (attributePositions.get(i) == c) {
                    if (!stopAttributes.contains(attr)) {
                        attributes.add(attr);
                    }
                    attributeNames.add(attr);
                    i++;
                }

            }
        } else if (r > 3) {
            Iterator<String> it = csvRecord.iterator();
            String uri = it.next();
            /*if (entities.contains(uri)) {
             System.out.println(uri + " already processed");
             continue;
             }*/
            entities.add(uri);
            it.next(); //skip rdf-schema#label
            it.next(); //skip rdf-schema#comment
            int c = 2;
            int i = 0;
            while (i < attributePositions.size()) {
                c++;
                String val = it.next();
                if (attributePositions.get(i) == c) {
                    if (!val.equalsIgnoreCase("null")) {
                        String attribute = attributeNames.get(i);
                        if (!stopAttributes.contains(attribute)) {
                            Integer ac = attributeCount.get(attribute);
                            if (ac == null) {
                                attributeCount.put(attribute, 1);
                            } else {
                                attributeCount.put(attribute, ac + 1);
                            }

                            Integer tcac = thisCategoryAttributeCounts.get(attribute);
                            if (tcac == null) {
                                thisCategoryAttributeCounts.put(attribute, 1);
                            } else {
                                thisCategoryAttributeCounts.put(attribute, tcac + 1);
                            }

                            HashMap<String, Integer> thisAttributeCategoryCounts = attributeCategoryCount
                                    .get(attribute);
                            if (thisAttributeCategoryCounts == null) {
                                thisAttributeCategoryCounts = new HashMap<>();
                                attributeCategoryCount.put(attribute, thisAttributeCategoryCounts);
                            }
                            Integer tacc = thisAttributeCategoryCounts.get(category);
                            if (tacc == null) {
                                thisAttributeCategoryCounts.put(category, 1);
                            } else {
                                thisAttributeCategoryCounts.put(category, tacc + 1);
                            }
                        }
                    }
                    i++;
                }
            }
        }
        r++;
    }
    categoryCount.put(category, r - 3);
}