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:nl.utwente.trafficanalyzer.GeoTagger.java

public static List readCsvFile(File fileName) {

    FileReader fileReader = null;
    CSVParser csvFileParser = null;/* ww w  .  j ava 2s  . c  om*/

    //Create the CSVFormat object with the header mapping
    CSVFormat csvFileFormat = CSVFormat.DEFAULT;

    try {

        //initialize FileReader object
        fileReader = new FileReader(fileName);

        //initialize CSVParser object
        csvFileParser = new CSVParser(fileReader, csvFileFormat);

        //Get a list of CSV file records
        List csvRecords = csvFileParser.getRecords();

        return csvRecords;
    } catch (Exception e) {
        System.out.println("Error in CsvFileReader !!!");
        e.printStackTrace();

    } finally {
        try {
            fileReader.close();
            csvFileParser.close();
        } catch (IOException e) {
            System.out.println("Error while closing fileReader/csvFileParser !!!");
            e.printStackTrace();
        }
    }
    return null;

}

From source file:no.packdrill.android.sparkledroid.lib.parse.csvtsv.CSVParse.java

@Override
public void parse(Reader input) throws IOException
//------------------------------------------------
{
    CSVFormat format = CSVFormat.DEFAULT.withHeader();
    parser = new CSVParser(input, format);
    headerMap = parser.getHeaderMap();/*from   www  .  j  ava2s  . c  o m*/
    Set<Map.Entry<String, Integer>> ss = headerMap.entrySet();
    columns = new String[headerMap.size()];
    for (Map.Entry<String, Integer> e : ss)
        columns[e.getValue()] = e.getKey();
    it = parser.iterator();
}

From source file:no.packdrill.android.sparkledroid.lib.parse.csvtsv.TSVParse.java

@Override
public void parse(Reader input) throws IOException
//------------------------------------------------
{
    CSVFormat format = CSVFormat.TDF.withHeader().withCommentStart('%');
    parser = new CSVParser(input, format);
    headerMap = parser.getHeaderMap();//from w ww  .  ja v a2s  . co m
    Set<Map.Entry<String, Integer>> ss = headerMap.entrySet();
    columns = new String[headerMap.size()];
    for (Map.Entry<String, Integer> e : ss)
        columns[e.getValue()] = e.getKey();
    it = parser.iterator();
}

From source file:norbert.mynemo.dataimport.fileformat.input.MovieLensIdConverter.java

/**
 * Loads the mapping file./* w  ww . j a va  2 s. c  o  m*/
 *
 * <p>
 * The columns of the mapping file are:
 * <ol>
 * <li>MovieLens id of the movie</li>
 * <li>rating</li>
 * <li>average</li>
 * <li>IMDb id of the movie</li>
 * <li>title and year</li>
 * </ol>
 *
 * @param mappingFilepath the file that contains the mapping
 */
public MovieLensIdConverter(String mappingFilepath) throws IOException {
    checkArgument(new File(mappingFilepath).exists(), "The mapping file must exist.");

    CSVParser parser = new CSVParser(
            new CleanRatingsReader(new BufferedReader(new FileReader(mappingFilepath))), CSVFormat.MYSQL);

    mappings = new HashMap<>();
    for (CSVRecord record : parser) {
        if (record.size() != RECORD_SIZE) {
            parser.close();
            throw new IllegalStateException("Error: unable to parse the movie file \"" + mappingFilepath
                    + "\". A list of five tab separated values is expected. Approximate" + " line number: "
                    + record.getRecordNumber());
        }
        mappings.put(record.get(MOVIELENS_MOVIE_ID_INDEX), record.get(IMDB_MOVIE_ID_INDEX));
    }

    parser.close();
}

From source file:norbert.mynemo.dataimport.fileformat.input.MovieLensRatingImporter.java

private static CSVParser createParser(String filepath) throws IOException {
    return new CSVParser(new CleanRatingsReader(new BufferedReader(new FileReader(filepath))), CSVFormat.MYSQL);
}

From source file:norbert.mynemo.dataimport.fileformat.MynemoRating.java

/**
 * Returns a parser able to read a Mynemo rating file.
 *
 * @param filepath file to read/*from  ww w  .  j a  v  a  2  s.c om*/
 * @return a new parser
 */
public static CSVParser createParser(String filepath) throws IOException {
    return new CSVParser(new BufferedReader(new FileReader(filepath)), CSV_FORMAT);
}

From source file:norbert.mynemo.dataimport.scraping.CkMapping.java

/**
 * Returns a parser able to read a CK movie mapping file.
 *
 * @param filepath file to read/* ww  w.j a v a 2 s  .  c  o m*/
 * @return a new parser
 */
public static CSVParser createParser(String filepath) throws IOException {
    return new CSVParser(new BufferedReader(new FileReader(filepath)), CSV_FORMAT_FOR_PARSER);
}

From source file:nz.ac.waikato.cms.supernova.gui.Supernova.java

/**
 * Generates the output of the "batch" tab.
 */// w w w . j ava  2 s  .c o m
protected void generateBatchOutput() {
    String cls;
    AbstractOutputGenerator generator;
    int colID;
    int colMeasure;
    int colScore;
    int colPercentile;
    Reader reader;
    CSVParser csvparser;
    String oldID;
    Map<String, List<Double>> test;
    String id;
    File outfile;
    String msg;
    String measure;
    double score;
    double percentile;
    String error;

    m_BatchLog.setText("");
    m_BatchGenerate.setEnabled(false);

    try {
        cls = AbstractOutputGenerator.class.getPackage().getName() + "." + m_SingleGenerator.getSelectedItem();
        generator = (AbstractOutputGenerator) Class.forName(cls).newInstance();
    } catch (Exception e) {
        batchLog("Failed to instantiate output generator - falling back on PNG", e);
        generator = new PNG();
    }

    try {
        colID = 0;
        colMeasure = 1;
        colScore = 2;
        colPercentile = 3;
        reader = new FileReader(m_BatchCSV.getCurrent());
        csvparser = new CSVParser(reader, CSVFormat.EXCEL.withHeader());
        oldID = "";
        test = new HashMap<>();
        for (CSVRecord rec : csvparser) {
            if (rec.size() < 4)
                continue;
            id = rec.get(colID);
            if (!id.equals(oldID)) {
                if (!test.isEmpty()) {
                    outfile = new File(m_BatchOutput.getCurrent() + File.separator + oldID + "."
                            + generator.getExtension());
                    batchLog("Generating: " + outfile, false);
                    batchLog("Using: " + test, false);
                    msg = generator.generate(test, outfile);
                    if (msg != null) {
                        error = "Failed to generate output for ID: " + oldID;
                        batchLog(error, true);
                    }
                }
                test.clear();
                oldID = id;
            }
            measure = rec.get(colMeasure);
            score = Double.parseDouble(rec.get(colScore));
            percentile = Double.parseDouble(rec.get(colPercentile));
            test.put(measure, new ArrayList<>(Arrays.asList(new Double[] { score, percentile })));
        }
        if (!test.isEmpty()) {
            outfile = new File(
                    m_BatchOutput.getCurrent() + File.separator + oldID + "." + generator.getExtension());
            batchLog("Generating: " + outfile, false);
            batchLog("Using: " + test, false);
            msg = generator.generate(test, outfile);
            if (msg != null) {
                error = "Failed to generate output for ID: " + oldID;
                batchLog(error, true);
            }
        }
    } catch (Exception e) {
        batchLog("Failed to generate output!", e);
    }

    m_BatchGenerate.setEnabled(true);
}

From source file:nz.ac.waikato.cms.supernova.SupernovaCSV.java

public static void main(String[] args) throws Exception {
    ArgumentParser parser;//from   ww  w.  j  av  a 2  s  .  c  om

    parser = ArgumentParsers.newArgumentParser("I am supernova");
    parser.description("Generates output according to 'I am supernova' by Keith Soo.\n"
            + "Loads scores/percentiles from a CSV file to generate multiple outputs at once.\n"
            + "Expected four columns (name of column is irrelevant):\n"
            + "- ID: the filename (excluding path and extension)\n" + "- Measure: the measure (" + MEASURE_LIST
            + ")\n" + "- Score: the score of the measure\n" + "- Percentile: the percentile of the measure\n"
            + "\n" + "Project homepage:\n" + "https://github.com/fracpete/i-am-supernova");

    // colors
    parser.addArgument("--" + AbstractOutputGenerator.OPENNESS + COLOR_SUFFIX)
            .metavar(AbstractOutputGenerator.OPENNESS + COLOR_SUFFIX).type(String.class)
            .setDefault(ColorHelper.toHex(Color.ORANGE))
            .help("The color for '" + AbstractOutputGenerator.OPENNESS + "' in hex format (e.g., "
                    + ColorHelper.toHex(Color.ORANGE) + ").");
    parser.addArgument("--" + AbstractOutputGenerator.EXTRAVERSION + COLOR_SUFFIX)
            .metavar(AbstractOutputGenerator.EXTRAVERSION + COLOR_SUFFIX).type(String.class)
            .setDefault(ColorHelper.toHex(Color.YELLOW))
            .help("The color for '" + AbstractOutputGenerator.EXTRAVERSION + "' in hex format (e.g., "
                    + ColorHelper.toHex(Color.YELLOW) + ").");
    parser.addArgument("--" + AbstractOutputGenerator.AGREEABLENESS + COLOR_SUFFIX)
            .metavar(AbstractOutputGenerator.AGREEABLENESS + COLOR_SUFFIX).type(String.class)
            .setDefault(ColorHelper.toHex(Color.GREEN))
            .help("The color for '" + AbstractOutputGenerator.AGREEABLENESS + "' in hex format (e.g., "
                    + ColorHelper.toHex(Color.GREEN) + ").");
    parser.addArgument("--" + AbstractOutputGenerator.CONSCIENTIOUSNESS + COLOR_SUFFIX)
            .metavar(AbstractOutputGenerator.CONSCIENTIOUSNESS + COLOR_SUFFIX).type(String.class)
            .setDefault(ColorHelper.toHex(Color.BLUE))
            .help("The color for '" + AbstractOutputGenerator.CONSCIENTIOUSNESS + "' in hex format (e.g., "
                    + ColorHelper.toHex(Color.BLUE) + ").");
    parser.addArgument("--" + AbstractOutputGenerator.NEUROTICISM + COLOR_SUFFIX)
            .metavar(AbstractOutputGenerator.NEUROTICISM + COLOR_SUFFIX).type(String.class)
            .setDefault(ColorHelper.toHex(Color.RED))
            .help("The color for '" + AbstractOutputGenerator.NEUROTICISM + "' in hex format (e.g., "
                    + ColorHelper.toHex(Color.RED) + ").");

    // other parameters
    parser.addArgument("--" + CSV).metavar(CSV).type(String.class).required(true)
            .help("The CSV file containing the scores/percentiles (header must be present).");

    parser.addArgument("--" + ID).metavar(ID).type(Integer.class).setDefault(1)
            .help("The 1-based index of the column in the CSV file containing the ID for the output file.");

    parser.addArgument("--" + MEASURE).metavar(MEASURE).type(Integer.class).setDefault(2)
            .help("The 1-based index of the column in the CSV file containing the measure name.\n"
                    + "Allowed values: " + MEASURE_LIST);

    parser.addArgument("--" + SCORE).metavar(SCORE).type(Integer.class).setDefault(3)
            .help("The 1-based index of the column in the CSV file containing the scores.");

    parser.addArgument("--" + PERCENTILE).metavar(PERCENTILE).type(Integer.class).setDefault(4)
            .help("The 1-based index of the column in the CSV file containing the percentiles.");

    parser.addArgument("--" + BACKGROUND).metavar(BACKGROUND).type(String.class)
            .setDefault(ColorHelper.toHex(Color.BLACK)).help("The background color.");

    parser.addArgument("--" + OPACITY).metavar(OPACITY).type(Double.class).setDefault(0.1)
            .help("The opacity (0-1).");

    parser.addArgument("--" + MARGIN).metavar(MARGIN).type(Double.class).setDefault(0.2)
            .help("The margin in the output (0-1).");

    parser.addArgument("--" + WIDTH).metavar(WIDTH).type(Integer.class).setDefault(2000)
            .help("The width of the output.");

    parser.addArgument("--" + HEIGHT).metavar(HEIGHT).type(Integer.class).setDefault(2000)
            .help("The height of the output.");

    parser.addArgument("--" + CENTER).metavar(CENTER).type(String.class).setDefault(Incenter.class.getName())
            .help("The name of the algorithm for calculating the center of a triangle.\n" + "Available: "
                    + Registry.toString(Registry.getCenters(), true));

    parser.addArgument("--" + GENERATOR).metavar(GENERATOR).type(String.class).setDefault(PNG.class.getName())
            .help("The name of the generator class to use.\n" + "Available: "
                    + Registry.toString(Registry.getGenerators(), true));

    parser.addArgument("--" + OUTPUT).metavar(OUTPUT).type(String.class)
            .help("The directory to store the output in.");

    parser.addArgument("--" + VERBOSE).metavar(VERBOSE).type(Boolean.class).action(Arguments.storeTrue())
            .help("Whether to output logging information.");

    Namespace namespace;
    try {
        namespace = parser.parseArgs(args);
    } catch (Exception e) {
        if (!(e instanceof HelpScreenException))
            parser.printHelp();
        return;
    }

    // colors
    Map<String, Color> colors = new HashMap<>();
    colors.put(AbstractOutputGenerator.OPENNESS, ColorHelper
            .valueOf(namespace.getString(AbstractOutputGenerator.OPENNESS + COLOR_SUFFIX), Color.ORANGE));
    colors.put(AbstractOutputGenerator.EXTRAVERSION, ColorHelper
            .valueOf(namespace.getString(AbstractOutputGenerator.EXTRAVERSION + COLOR_SUFFIX), Color.YELLOW));
    colors.put(AbstractOutputGenerator.AGREEABLENESS, ColorHelper
            .valueOf(namespace.getString(AbstractOutputGenerator.AGREEABLENESS + COLOR_SUFFIX), Color.GREEN));
    colors.put(AbstractOutputGenerator.CONSCIENTIOUSNESS, ColorHelper.valueOf(
            namespace.getString(AbstractOutputGenerator.CONSCIENTIOUSNESS + COLOR_SUFFIX), Color.BLUE));
    colors.put(AbstractOutputGenerator.NEUROTICISM, ColorHelper
            .valueOf(namespace.getString(AbstractOutputGenerator.NEUROTICISM + COLOR_SUFFIX), Color.RED));

    File outdir = new File(namespace.getString(OUTPUT));

    String centerCls = namespace.getString(CENTER);
    if (!centerCls.contains("."))
        centerCls = AbstractTriangleCenterCalculation.class.getPackage().getName() + "." + centerCls;
    String generatorCls = namespace.getString(GENERATOR);
    if (!generatorCls.contains("."))
        generatorCls = AbstractOutputGenerator.class.getPackage().getName() + "." + generatorCls;
    AbstractOutputGenerator generator = (AbstractOutputGenerator) Class.forName(generatorCls).newInstance();
    generator.setVerbose(namespace.getBoolean(VERBOSE));
    generator.setColors(colors);
    generator.setBackground(ColorHelper.valueOf(namespace.getString(BACKGROUND), Color.BLACK));
    generator.setOpacity(namespace.getDouble(OPACITY));
    generator.setMargin(namespace.getDouble(MARGIN));
    generator.setCenter((AbstractTriangleCenterCalculation) Class.forName(centerCls).newInstance());
    if (generator instanceof AbstractOutputGeneratorWithDimensions) {
        AbstractOutputGeneratorWithDimensions pixel = (AbstractOutputGeneratorWithDimensions) generator;
        pixel.setWidth(namespace.getInt(WIDTH));
        pixel.setHeight(namespace.getInt(HEIGHT));
    }

    int colID = namespace.getInt(ID) - 1;
    int colMeasure = namespace.getInt(MEASURE) - 1;
    int colScore = namespace.getInt(SCORE) - 1;
    int colPercentile = namespace.getInt(PERCENTILE) - 1;
    Reader reader = new FileReader(namespace.getString(CSV));
    CSVParser csvparser = new CSVParser(reader, CSVFormat.EXCEL.withHeader());
    String oldID = "";
    Map<String, List<Double>> test = new HashMap<>();
    for (CSVRecord rec : csvparser) {
        if (rec.size() < 4)
            continue;
        String id = rec.get(colID);
        if (!id.equals(oldID)) {
            if (!test.isEmpty()) {
                File outfile = new File(outdir + File.separator + oldID + "." + generator.getExtension());
                String msg = generator.generate(test, outfile);
                if (msg != null)
                    System.err.println("Failed to generate output for ID: " + oldID);
            }
            test.clear();
            oldID = id;
        }
        String measure = rec.get(colMeasure);
        double score = Double.parseDouble(rec.get(colScore));
        double percentile = Double.parseDouble(rec.get(colPercentile));
        test.put(measure, new ArrayList<>(Arrays.asList(new Double[] { score, percentile })));
    }
    if (!test.isEmpty()) {
        File outfile = new File(outdir + File.separator + oldID + "." + generator.getExtension());
        String msg = generator.generate(test, outfile);
        if (msg != null)
            System.err.println("Failed to generate output for ID: " + oldID);
    }
}

From source file:nzilbb.agcsv.AgCsvDeserializer.java

/**
 * Loads the serialized form of the graph, using the given set of named streams.
 * @param streams A list of named streams that contain all the transcription/annotation data required.
 * @param schema The layer schema, definining layers and the way they interrelate.
 * @return A list of parameters that require setting before {@link IDeserializer#deserialize()} can be invoked. This may be an empty list, and may include parameters with the value already set to a workable default. If there are parameters, and user interaction is possible, then the user may be presented with an interface for setting/confirming these parameters, before they are then passed to {@link IDeserializer#setParameters(ParameterSet)}.
 * @throws SerializationException If the graph could not be loaded.
 * @throws IOException On IO error./*  w  w w .ja v  a2 s  .c  o m*/
 * @throws SerializerNotConfiguredException If the configuration is not sufficient for deserialization.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public ParameterSet load(NamedStream[] streams, Schema schema)
        throws IOException, SerializationException, SerializerNotConfiguredException {
    if (getFieldDelimiter() == null)
        throw new SerializerNotConfiguredException("fieldDelimiter must be set.");
    ParameterSet parameters = new ParameterSet();

    // take the first csv stream, ignore all others.
    NamedStream csv = Utility.FindSingleStream(streams, ".csv", "text/csv");
    if (csv == null)
        throw new SerializationException("No CSV stream found");
    setName(csv.getName());
    setName(getName().replaceFirst("\\.csv$", "").replaceFirst("\\.ag$", ""));

    reset();

    CSVParser parser = new CSVParser(new InputStreamReader(csv.getStream()),
            CSVFormat.EXCEL.withDelimiter(fieldDelimiter.charAt(0)));
    mDiscoveredLayers = new HashMap<String, Layer>();
    Vector<CSVRecord> vRecords = new Vector<CSVRecord>();
    mCsvData.put("anchor", vRecords); // start with anchors

    // read all the lines, and extract the layer names
    for (CSVRecord line : parser) {
        // does it have only one field? - the layer name
        if (line.get(0).equals("layer")) {
            Layer layer = new Layer(line.get(1), line.get(2), Integer.parseInt(line.get(5)), true, // peers
                    false, // peersOverlap
                    false, // saturated
                    line.get(4).equals("W") ? schema.getWordLayerId() // parentId
                            : line.get(4).equals("M") ? schema.getTurnLayerId() // parentId
                                    : line.get(4).equals("F") ? "graph" : "segments", // parentId
                    true); // parentIncludes
            int layerId = Integer.parseInt(line.get(6));
            if (layerId == 11) // turn
            {
                layer.setParentId(schema.getParticipantLayerId());
            } else if (layerId == 12) // utterance
            {
                layer.setSaturated(true);
            } else if (layerId == 0) // transcription
            {
                layer.setParentId(schema.getTurnLayerId());
            } else if (layerId == 2) // orthography
            {
                layer.setPeers(false);
                layer.setSaturated(true);
            } else if (layerId == 1) // segments
            {
                layer.setSaturated(true);
            }
            layer.put("@layer_id", layerId);
            layer.put("@type", line.get(3));
            layer.put("@scope", line.get(4));
            mDiscoveredLayers.put(line.get(1), layer);
            Parameter p = new Parameter(layer.getId(), Layer.class, layer.getId(), layer.getDescription(),
                    true);
            p.setValue(schema.getLayer(layer.getId()));
            p.setPossibleValues(schema.getLayers().values());
            parameters.addParameter(p);

            // start a new set of records
            vRecords = new Vector<CSVRecord>();
            mCsvData.put(layer.getId(), vRecords);
        }
        vRecords.add(line);
    } // next line
    parser.close();

    return parameters;
}