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

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

Introduction

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

Prototype

@Override
public String toString() 

Source Link

Document

Returns a string representation of the contents of this record.

Usage

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  a2 s. c o  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:citation_prediction.CitationCore.java

/**
 * Get CSV citation history from a file.
 * //from  www .ja  v a  2  s . c  o  m
 * @param filename The filename and path containing the citation data.
 * @param format The format of the file.
 * @param hasHeader Does the file have a line with headings?
 * @return A record containing the csv information.
 */
private static List<CSVRecord> getCSVData(String filename, CSVFormat format, boolean hasHeader) {

    boolean error = true;
    List<CSVRecord> list_ourdata = null;

    try {
        FileReader ourdata = new FileReader(filename);

        CSVParser data_parser = new CSVParser(ourdata, format);

        list_ourdata = data_parser.getRecords();

        if (hasHeader) {
            list_ourdata.remove(0);
        } //remove header file.

        Iterator<CSVRecord> list_iterator = list_ourdata.iterator();
        for (int rowIndex = 0; rowIndex < list_ourdata.size(); rowIndex++) {
            CSVRecord record = list_iterator.next();

            System.out.println("#" + (rowIndex + 1) + " " + record.toString());

        }

        data_parser.close();
        ourdata.close();

        error = false;

    } catch (java.io.FileNotFoundException e) {
        System.err.println("ERROR: There was an error opening, reading, or parsing the input file.");
        System.err.println("ERROR:" + filename);
        error = true;
    } catch (java.io.IOException e) {
        System.err.println("ERROR: Could not close the parser or the input file.");
        error = true;
    }

    if (error || list_ourdata == null) {
        System.exit(1);
        return null;
    } else {
        return list_ourdata;
    }
}

From source file:loternik.MyService.java

public String getHelloMessage(String teryt, String okreg) {
    FileReader file = null;//from ww w. ja va 2 s. co  m
    try {
        String output = "";

        file = new FileReader("kandydaci.csv");
        CSVParser parser = new CSVParser(file, CSVFormat.EXCEL.withHeader().withDelimiter(';'));
        for (CSVRecord csvRecord : parser) {
            if (teryt.equals(csvRecord.get("teryt")) && okreg.equals(csvRecord.get("Okreg"))) {
                output += "<p>" + csvRecord.toString() + "</p>";
            }
        }
        return output;
    } catch (FileNotFoundException ex) {
        Logger.getLogger(MyService.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(MyService.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            file.close();
        } catch (IOException ex) {
            Logger.getLogger(MyService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return null;
}

From source file:de.inren.service.banking.BankDataServiceImpl.java

@Override
public void importFinanceCsv(byte[] bytes) throws IOException {
    Iterable<CSVRecord> records = getIngDibaCsvFormat().parse(createReader(bytes));
    for (CSVRecord record : records) {
        log.info(record.toString());
    }//from   www  .  j a  v a2 s.c om
}

From source file:com.cotrino.langnet.GenerateVisualization.java

private double[] calculateSimilarity(File logFile) throws IOException {

    int i = 0;// w ww  . j a va2s . co m
    double total = 0.0;
    Reader reader = new FileReader(logFile);
    CSVParser parser = new CSVParser(reader, csvFormat);
    for (CSVRecord record : parser) {
        try {
            double similarity = Double.parseDouble(record.get("Similarity"));
            total += similarity;
            i++;
        } catch (NumberFormatException e) {
            logger.error("At " + logFile.getName() + ", failed line: " + record.toString());
        }
    }
    parser.close();
    reader.close();
    return new double[] { (total / i), i };

}

From source file:geovista.readers.csv.GeogCSVReader_old.java

public Object[] readFileStreaming(InputStream is, ArrayList<Integer> columns) {

    BufferedReader in = new BufferedReader(new InputStreamReader(is));
    Iterable<CSVRecord> parser = null;
    try {/*from w  w w. j  a v a  2  s .  co m*/
        parser = CSVFormat.DEFAULT.withDelimiter(this.delimiter).parse(in);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    int count = 0;
    for (CSVRecord rec : parser) {

        // eDays.add(rec.get(0));
        // type.add(rec.get(10) + " - " + rec.get(8));

        System.out.println(rec.get(0));
        System.out.println(rec.toString());
        count++;
    }
    // CSVParser shredder = new CSVParser()
    // CSVParser shredder = new CSVParser(is);

    // shredder.setCommentStart("#;!");
    // shredder.setEscapes("nrtf", "\n\r\t\f");
    String[] headers = null;
    String[] types = null;
    int[] dataTypes = null;
    String[][] fileContent = null;
    int dataBegin;
    Object[] data;
    try {
        // fileContent = shredder.getAllValues();

    } catch (Exception ex) {
        ex.printStackTrace();
    }

    types = fileContent[0];// first line tells us types
    dataTypes = new int[types.length];
    int len;
    if (types[0].equalsIgnoreCase("int") || types[0].equalsIgnoreCase("double")
            || types[0].equalsIgnoreCase("string")) {
        dataBegin = 2;
        headers = fileContent[1];
        data = new Object[headers.length + 1];// plus one for the headers
        // themselves
        len = fileContent.length - dataBegin;
        for (int i = 0; i < headers.length; i++) {
            if (types[i].equalsIgnoreCase("int")) {
                data[i + 1] = new int[len];
                dataTypes[i] = GeogCSVReader_old.DATA_TYPE_INT;
            } else if (types[i].equalsIgnoreCase("double")) {
                data[i + 1] = new double[len];
                dataTypes[i] = GeogCSVReader_old.DATA_TYPE_DOUBLE;
            } else if (types[i].equalsIgnoreCase("string")) {
                data[i + 1] = new String[len];
                dataTypes[i] = GeogCSVReader_old.DATA_TYPE_STRING;
            } else {
                throw new IllegalArgumentException("GeogCSVReader.readFile, unknown type = " + types[i]);
            }
        }
    } else {
        dataBegin = 1;
        headers = fileContent[0];
        data = new Object[headers.length + 1];// plus one for the headers
        // themselves
        len = fileContent.length - dataBegin;
        for (int i = 0; i < headers.length; i++) {
            String firstString = fileContent[1][i];
            String secondString = fileContent[2][i];
            String thirdString = fileContent[3][i];
            String lastString = fileContent[fileContent[0].length][i];

            if (isNumeric(firstString) && isNumeric(secondString) && isNumeric(thirdString)
                    && isNumeric(lastString)) {
                if (isInt(fileContent, i) == false) {
                    // if (isDouble(firstString) || isDouble(secondString)
                    // || isDouble(thirdString) || isDouble(lastString)) {
                    data[i + 1] = new double[len];
                    dataTypes[i] = GeogCSVReader_old.DATA_TYPE_DOUBLE;
                } else {
                    data[i + 1] = new int[len];
                    dataTypes[i] = GeogCSVReader_old.DATA_TYPE_INT;
                }
            } else {
                data[i + 1] = new String[len];
                dataTypes[i] = GeogCSVReader_old.DATA_TYPE_STRING;
            }
        }
    }
    data[0] = headers;

    String[] line = null;

    for (int row = dataBegin; row < len + dataBegin; row++) {

        line = fileContent[row];

        int[] ints = null;
        double[] doubles = null;
        String[] strings = null;

        for (int column = 0; column < line.length; column++) {
            String item = line[column];
            if (dataTypes[column] == GeogCSVReader_old.DATA_TYPE_INT) {

                if (Arrays.binarySearch(GeogCSVReader_old.NULL_STRINGS, item) >= 0) {
                    ints = (int[]) data[column + 1];
                    ints[row - dataBegin] = GeogCSVReader_old.NULL_INT;
                } else {
                    ints = (int[]) data[column + 1];
                    try {
                        ints[row - dataBegin] = Integer.parseInt(item);
                    } catch (NumberFormatException nfe) {
                        logger.warning("could not parse " + item + " in column " + column);
                        // nfe.printStackTrace();
                        ints[row - dataBegin] = GeogCSVReader_old.NULL_INT;
                    }
                }
            } else if (dataTypes[column] == GeogCSVReader_old.DATA_TYPE_DOUBLE) {
                if (Arrays.binarySearch(GeogCSVReader_old.NULL_STRINGS, item) >= 0) {
                    doubles = (double[]) data[column + 1];
                    doubles[row - dataBegin] = GeogCSVReader_old.NULL_DOUBLE;
                } else {
                    doubles = (double[]) data[column + 1];
                    doubles[row - dataBegin] = parseDouble(item);
                }
            } else if (dataTypes[column] == GeogCSVReader_old.DATA_TYPE_STRING) {
                strings = (String[]) data[column + 1];
                strings[row - dataBegin] = item;
            } else {
                throw new IllegalArgumentException("GeogCSVReader.readFile, unknown type = " + types[row]);
            } // end if

        } // next column
    } // next row
    return data;

}

From source file:geovista.readers.csv.GeogCSVReader.java

public Object[] readFileStreaming(InputStream is, ArrayList<Integer> columns) {

    BufferedReader in = new BufferedReader(new InputStreamReader(is));
    Iterable<CSVRecord> parser = null;
    try {/*  ww w .  ja v a 2s .  c om*/
        parser = CSVFormat.DEFAULT.withDelimiter(this.currDelimiter).parse(in);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    int count = 0;
    for (CSVRecord rec : parser) {

        // eDays.add(rec.get(0));
        // type.add(rec.get(10) + " - " + rec.get(8));

        System.out.println(rec.get(0));
        System.out.println(rec.toString());
        count++;
    }
    // CSVParser shredder = new CSVParser()
    // CSVParser shredder = new CSVParser(is);

    // shredder.setCommentStart("#;!");
    // shredder.setEscapes("nrtf", "\n\r\t\f");
    String[] headers = null;
    String[] types = null;
    int[] dataTypes = null;
    String[][] fileContent = null;
    int dataBegin;
    Object[] data;
    try {
        // fileContent = shredder.getAllValues();

    } catch (Exception ex) {
        ex.printStackTrace();
    }

    types = fileContent[0];// first line tells us types
    dataTypes = new int[types.length];
    int len;
    if (types[0].equalsIgnoreCase("int") || types[0].equalsIgnoreCase("double")
            || types[0].equalsIgnoreCase("string")) {
        dataBegin = 2;
        headers = fileContent[1];
        data = new Object[headers.length + 1];// plus one for the headers
        // themselves
        len = fileContent.length - dataBegin;
        for (int i = 0; i < headers.length; i++) {
            if (types[i].equalsIgnoreCase("int")) {
                data[i + 1] = new int[len];
                dataTypes[i] = GeogCSVReader.DATA_TYPE_INT;
            } else if (types[i].equalsIgnoreCase("double")) {
                data[i + 1] = new double[len];
                dataTypes[i] = GeogCSVReader.DATA_TYPE_DOUBLE;
            } else if (types[i].equalsIgnoreCase("string")) {
                data[i + 1] = new String[len];
                dataTypes[i] = GeogCSVReader.DATA_TYPE_STRING;
            } else {
                throw new IllegalArgumentException("GeogCSVReader.readFile, unknown type = " + types[i]);
            }
        }
    } else {
        dataBegin = 1;
        headers = fileContent[0];
        data = new Object[headers.length + 1];// plus one for the headers
        // themselves
        len = fileContent.length - dataBegin;
        for (int i = 0; i < headers.length; i++) {
            String firstString = fileContent[1][i];
            String secondString = fileContent[2][i];
            String thirdString = fileContent[3][i];
            String lastString = fileContent[fileContent[0].length][i];

            if (isNumeric(firstString) && isNumeric(secondString) && isNumeric(thirdString)
                    && isNumeric(lastString)) {
                if (isInt(fileContent, i) == false) {
                    // if (isDouble(firstString) || isDouble(secondString)
                    // || isDouble(thirdString) || isDouble(lastString)) {
                    data[i + 1] = new double[len];
                    dataTypes[i] = GeogCSVReader.DATA_TYPE_DOUBLE;
                } else {
                    data[i + 1] = new int[len];
                    dataTypes[i] = GeogCSVReader.DATA_TYPE_INT;
                }
            } else {
                data[i + 1] = new String[len];
                dataTypes[i] = GeogCSVReader.DATA_TYPE_STRING;
            }
        }
    }
    data[0] = headers;

    String[] line = null;

    for (int row = dataBegin; row < len + dataBegin; row++) {

        line = fileContent[row];

        int[] ints = null;
        double[] doubles = null;
        String[] strings = null;

        for (int column = 0; column < line.length; column++) {
            String item = line[column];
            if (dataTypes[column] == GeogCSVReader.DATA_TYPE_INT) {

                if (Arrays.binarySearch(GeogCSVReader.NULL_STRINGS, item) >= 0) {
                    ints = (int[]) data[column + 1];
                    ints[row - dataBegin] = GeogCSVReader.NULL_INT;
                } else {
                    ints = (int[]) data[column + 1];
                    try {
                        ints[row - dataBegin] = Integer.parseInt(item);
                    } catch (NumberFormatException nfe) {
                        logger.warning("could not parse " + item + " in column " + column);
                        // nfe.printStackTrace();
                        ints[row - dataBegin] = GeogCSVReader.NULL_INT;
                    }
                }
            } else if (dataTypes[column] == GeogCSVReader.DATA_TYPE_DOUBLE) {
                if (Arrays.binarySearch(GeogCSVReader.NULL_STRINGS, item) >= 0) {
                    doubles = (double[]) data[column + 1];
                    doubles[row - dataBegin] = GeogCSVReader.NULL_DOUBLE;
                } else {
                    doubles = (double[]) data[column + 1];
                    doubles[row - dataBegin] = parseDouble(item);
                }
            } else if (dataTypes[column] == GeogCSVReader.DATA_TYPE_STRING) {
                strings = (String[]) data[column + 1];
                strings[row - dataBegin] = item;
            } else {
                throw new IllegalArgumentException("GeogCSVReader.readFile, unknown type = " + types[row]);
            } // end if

        } // next column
    } // next row
    return data;

}

From source file:convertCSV.ConvertCSV.java

/**
 * @param fichier_// w ww. j a v a2  s. com
 * @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:edu.isi.misd.scanner.network.modules.worker.processors.ptr.PrepToResearchProcessor.java

private PrepToResearchResponse analyzeFile(PrepToResearchRequest request, File analysisFile) throws Exception {
    PrepToResearchResponse response = new PrepToResearchResponse();
    Integer requestedOmopConceptID = request.getOmopConceptID();
    CSVFormat csvFormat = CSVFormat.newFormat(',').withHeader().withCommentMarker('#').withQuote('"');
    CSVParser parser = CSVParser.parse(analysisFile, Charset.defaultCharset(), csvFormat);
    for (CSVRecord csvRecord : parser) {
        try {/*  ww  w .ja va2 s.  c o m*/
            this.validateCSVRecord(csvRecord);

            // check the ID first, if no match continue
            Integer omopConceptID = Integer
                    .parseInt(csvRecord.get(ExpectedColumnName.OMOP_CONCEPT_ID.toString()));
            if (!requestedOmopConceptID.equals(omopConceptID)) {
                continue;
            }

            // match found, create response output record
            if (log.isDebugEnabled()) {
                log.debug(String.format("Found a match for requested ID %s, record: %s", requestedOmopConceptID,
                        csvRecord.toString()));
            }
            PrepToResearchRecord ptrRecord = new PrepToResearchRecord();
            ptrRecord.setOmopConceptID(omopConceptID);
            ptrRecord.setOmopConceptName(csvRecord.get(ExpectedColumnName.OMOP_CONCEPT_NAME));
            ptrRecord.setCategory(csvRecord.get(ExpectedColumnName.CATEGORY));
            ptrRecord.setCategoryValue(csvRecord.get(ExpectedColumnName.CATEGORY_VALUE));
            ptrRecord.setCountFemales(Integer.parseInt(csvRecord.get(ExpectedColumnName.COUNT_FEMALES)));
            ptrRecord.setCountMales(Integer.parseInt(csvRecord.get(ExpectedColumnName.COUNT_MALES)));
            ptrRecord.setCountTotal(Integer.parseInt(csvRecord.get(ExpectedColumnName.COUNT_TOTAL)));

            response.getPrepToResearchRecord().add(ptrRecord);
        } catch (Exception e) {
            String error = String.format(
                    "An exception occured while processing row number %s with the following values %s: %s",
                    csvRecord.getRecordNumber(), csvRecord.toString(), e.toString());
            parser.close();
            throw new RuntimeException(error);
        }
    }
    parser.close();
    return response;
}

From source file:ca.liquidlabs.android.speedtestvisualizer.model.SpeedTestRecord.java

/**
 * Constructs speedtest model object from parsed csv record. <br/>
 * TODO: Handle the exceptions in future, and show user friendly error
 * message to user./*from   w w w  .  j  a v a  2  s .c om*/
 * 
 * @param csvRecord
 */
public SpeedTestRecord(CSVRecord csvRecord) {
    try {
        this.date = csvRecord.get(KEY_DATE);

        // data connection type - should be one of expected values
        this.connectionType = ConnectionType.fromString(csvRecord.get(KEY_CONNTYPE));

        // Lat, Lon is in float
        this.lat = Float.parseFloat(csvRecord.get(KEY_LAT));
        this.lon = Float.parseFloat(csvRecord.get(KEY_LON));

        // download and upload values are always in kbps
        this.download = Float.parseFloat(csvRecord.get(KEY_DOWNL));
        this.upload = Float.parseFloat(csvRecord.get(KEY_UPL));

        // latency is numeric - in milliseconds
        this.latency = Integer.parseInt(csvRecord.get(KEY_LATENCY));

        this.serverName = csvRecord.get(KEY_SERVER);
        this.internalIp = csvRecord.get(KEY_IPINT);
        this.externalIp = csvRecord.get(KEY_IPEXT);
    } catch (NumberFormatException e) {
        // if for some reason unexpected value is passed, stop parsing
        throw new IllegalArgumentException("Unable to parse record: " + csvRecord.toString());
    } catch (ArrayIndexOutOfBoundsException e) {
        // this might happen for some leftover lines when copy and pasting
        // data.
        throw new IllegalArgumentException("Invalid record : " + csvRecord.toString());
    }
}