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

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

Introduction

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

Prototype

public String get(final String name) 

Source Link

Document

Returns a value by name.

Usage

From source file:nl.utwente.trafficanalyzer.Grouper.java

public static void groupFilesInFolder(Path dir) {
    FileWriter writer = null;//from w  ww .  java 2s.com
    try {

        File folder = dir.toFile();
        File[] listOfFiles = folder.listFiles();
        Arrays.sort(listOfFiles);

        HashMap<String, List<File>> hmap = new HashMap<>();
        System.err.println("sorting files...");
        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                String streetname = listOfFiles[i].getName().split("_")[0];
                if (hmap.keySet().contains(streetname)) {
                    System.out.println("\tAdded new day for: " + streetname);
                    hmap.get(streetname).add(listOfFiles[i]);
                } else {
                    List<File> newList = new ArrayList<File>();
                    newList.add(listOfFiles[i]);
                    hmap.put(streetname, newList);
                    System.out.println("\tAdded new road : " + streetname);
                }
            }
        }

        System.err.println("generating csvs...");
        for (String street : hmap.keySet()) {
            System.out.println("generating file: " + dir.toString() + "\\" + street + ".csv");
            writer = new FileWriter(dir.toString() + "\\" + street + ".csv");
            for (int i = 0; i < hmap.get(street).size(); i++) {
                List list = readCsvFile(hmap.get(street).get(i));
                for (Object line : list) {
                    CSVRecord record = (CSVRecord) line;
                    for (int j = 0; j < 6; j++) {
                        writer.append(record.get(j));
                        writer.append(',');
                    }
                    //end line
                    writer.append('\n');
                }
            }
            writer.flush();
            writer.close();
        }
    } catch (IOException ex) {
        Logger.getLogger(Grouper.class.getName()).log(Level.SEVERE, null, ex);

    }
}

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

@Override
public Iterator<Map<String, Cell>> iterator()
//--------------------------------
{
    return new Iterator<Map<String, Cell>>()
    //========================================
    {//from   w w w .  j  a  v a2s .  c o m
        Map<String, Cell> m = new HashMap<String, Cell>();

        @Override
        public boolean hasNext() {
            return it.hasNext();
        }

        @Override
        public Map<String, Cell> next()
        //--------------------------------
        {
            CSVRecord record = null;
            try {
                record = it.next();
                m.clear();
                for (int i = 0; i < record.size(); i++) {
                    String k = columns[i];
                    final String v = record.get(i);
                    if (v == null)
                        continue;
                    if (v.trim().startsWith("_:")) {
                        int p = v.indexOf("_:");
                        String name;
                        try {
                            name = v.substring(p + 2);
                        } catch (Exception _e) {
                            name = "";
                        }
                        Cell ri = new Cell(true, name);
                        ri.setUnparsedValue(v);
                        m.put(k, ri);
                        continue;
                    }
                    if (v.trim().toLowerCase().startsWith("missing ")) {
                        Object o = null;
                        Cell ri = new Cell("null", o);
                        ri.setUnparsedValue(v);
                        m.put(k, ri);
                        continue;
                    }
                    URI uri = null;
                    try {
                        uri = new URI(v);
                    } catch (Exception _e) {
                        uri = null;
                    }
                    if (uri != null) {
                        Cell ri = processURI(v);
                        if (ri != null) {
                            m.put(k, ri);
                            continue;
                        }
                    }
                    Cell ri = new Cell(v);
                    ri.setUnparsedValue(v);
                    m.put(k, ri);
                }
            } catch (NoSuchElementException e) {
                lastError = (record == null) ? "" : record.toString();
                lastException = e;
                Log.e(LOGTAG, lastError, e);
                return null;
            }
            return m;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException(
                    "no.packdrill.android.SPARQLClient.parse.csvtsv.iterator.remove");
        }
    };
}

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

@Override
public Iterator<Map<String, Cell>> iterator()
//--------------------------------
{
    return new Iterator<Map<String, Cell>>()
    //========================================
    {//  ww w.  ja  v  a  2s  .  co m
        Map<String, Cell> m = new HashMap<String, Cell>();

        final Pattern languagePattern = Pattern.compile("\"(.+)\"@(\\w\\w)$");

        @Override
        public boolean hasNext() {
            return it.hasNext();
        }

        @Override
        public Map<String, Cell> next()
        //--------------------------------
        {
            CSVRecord record = null;
            int p;
            try {
                record = it.next();
                m.clear();
                for (int i = 0; i < record.size(); i++) {
                    String k = columns[i];
                    String v = record.get(i);
                    if (v == null)
                        continue;
                    if (v.trim().startsWith("_:")) {
                        p = v.indexOf("_:");
                        String name;
                        try {
                            name = v.substring(p + 2);
                        } catch (Exception _e) {
                            name = "";
                        }
                        Cell ri = new Cell(true, name);
                        ri.setUnparsedValue(v);
                        m.put(k, ri);
                        continue;
                    }
                    if ((v.trim().startsWith("<")) && (v.trim().endsWith(">"))) {
                        p = v.indexOf('<');
                        int p1 = v.indexOf('>');
                        if ((p >= 0) && (p1 > 0))
                            v = v.substring(p + 1, p1);
                        URI uri = null;
                        try {
                            uri = new URI(v);
                        } catch (Exception _e) {
                            uri = null;
                        }
                        if (uri != null) {
                            Cell ri = processURI(v);
                            if (ri != null) {
                                m.put(k, ri);
                                continue;
                            }
                        }

                        Matcher patmatch = languagePattern.matcher(v);
                        if ((patmatch.matches()) && (patmatch.groupCount() > 0)) {
                            String s = patmatch.group(1);
                            String lang = null;
                            if (patmatch.groupCount() > 1)
                                lang = patmatch.group(2);
                            if ((s != null) && (lang != null)) {
                                Cell ri = new Cell(s, lang);
                                ri.setUnparsedValue(v);
                                m.put(k, ri);
                                continue;
                            }
                        }

                    }
                    Cell ri = new Cell(v);
                    ri.setUnparsedValue(v);
                    m.put(k, ri);
                }
            } catch (Exception e) {
                lastError = (record == null) ? "" : record.toString();
                lastException = e;
                Log.e(LOGTAG, lastError, e);
                return null;
            }
            return m;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException(
                    "no.packdrill.android.SPARQLClient.parse.csvtsv.iterator.remove");
        }
    };
}

From source file:no.uio.medicine.virsurveillance.parsers.CSVsGBDdata.java

public void parse(String deathFolder) throws IOException {
    File f = new File(deathFolder);
    Runtime runtime = Runtime.getRuntime();
    if (f.isDirectory()) {
        String[] filesInDir = f.list();

        for (String fil : filesInDir) {
            if (fil.endsWith(".zip")) {
                ZipFile zipFile = new ZipFile(deathFolder + "/" + fil);

                Enumeration<? extends ZipEntry> entries = zipFile.entries();

                while (entries.hasMoreElements()) {
                    System.out.println(
                            "Used memory: " + (runtime.totalMemory() - runtime.freeMemory()) / (1024 * 1024)
                                    + " Free memory: " + (runtime.freeMemory()) / (1024 * 1024));

                    ZipEntry entry = entries.nextElement();
                    InputStream stream = zipFile.getInputStream(entry);
                    BufferedReader br = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
                    CSVParser parser = CSVFormat.RFC4180.withDelimiter(',').withIgnoreEmptyLines().withHeader()
                            .parse(br);//from w  w w  .j  a v  a2  s . c  om

                    List<CSVRecord> records = parser.getRecords();
                    System.out.println("Reading records: " + zipFile.getName() + "/" + entry);
                    /*for (int i=0;i<records.size();i++) {
                    CSVRecord csvRecord = records.get(i);*/
                    for (CSVRecord csvRecord : records) {
                        if (csvRecord.isMapped("age_group_id")) { //age group 22 corresponds to all ages
                            if (csvRecord.get("age_group_id").equalsIgnoreCase("22")) {
                                String location = null;
                                String year = null;
                                String sex = null;
                                String cause = null;
                                String number = null;
                                String metric = null;

                                if (csvRecord.isMapped("location_code")) {
                                    location = csvRecord.get("location_code");
                                }
                                if (csvRecord.isMapped("year")) {
                                    year = csvRecord.get("year");
                                }
                                if (csvRecord.isMapped("sex_id")) { //1=male, 2 = female
                                    if (csvRecord.get("sex_id").equalsIgnoreCase(("1"))) {
                                        sex = "m";
                                    } else if (csvRecord.get("sex_id").equalsIgnoreCase("2")) {
                                        sex = "f";
                                    }
                                }
                                if (csvRecord.isMapped("cause_name")) {
                                    cause = csvRecord.get("cause_name");
                                }
                                if (csvRecord.isMapped("mean")) {
                                    number = csvRecord.get("mean");
                                }
                                if (csvRecord.isMapped("metric") && csvRecord.isMapped("unit")) {
                                    metric = csvRecord.get("metric") + "-" + csvRecord.get("unit");
                                }

                                if (location != null && year != null && sex != null && cause != null
                                        && number != null && metric != null) {
                                    try {
                                        sqlM.addSanitaryIssueToCountry(location, year, sex, cause, metric,
                                                number);
                                    } catch (SQLException ex) {

                                        Logger.getLogger(CSVsGBDdata.class.getName()).log(Level.SEVERE, null,
                                                ex);
                                    }
                                }

                            }

                        }

                    }

                    parser.close();

                    stream.close();
                    br.close();
                }
                zipFile.close();
            }
        }
    } else {
        System.out.println("Not a directory");
    }
}

From source file:no.uio.medicine.virsurveillance.parsers.CSVVirusNames.java

public ArrayList<String> getVirusNames(String inputVirusFile) throws FileNotFoundException, IOException {
    ArrayList<String> viruses = new ArrayList<>();

    BufferedReader reader = new BufferedReader(new FileReader(inputVirusFile));
    CSVParser parser = CSVFormat.RFC4180.withDelimiter(';').withIgnoreEmptyLines().withHeader().parse(reader);
    for (CSVRecord csvRecord : parser) {
        if (csvRecord.isMapped("Virus Name")) {
            String currentVirus = csvRecord.get("Virus Name").replace(" virus", "").replace("", "%%");
            viruses.add(currentVirus);/*from   w w w.  j  ava 2s.co  m*/
        }
    }

    return viruses;

}

From source file:no.uio.medicine.virsurveillance.parsers.CsvWosParser.java

public void parse(String inputPath) {

    try {/* ww w .j  ava2  s. c o m*/
        int year = 2014;//default value
        BufferedReader reader = new BufferedReader(new FileReader(inputPath));
        String line = reader.readLine(); // Read the first/current line.
        try {
            if (line.contains("Selected JCR Year:")) {
                String[] splitedLine = line.split("Selected JCR Year:");
                year = Integer.parseInt(splitedLine[1].split(" ")[1]);
            }
        } catch (Exception e) {
            System.out.println("Journal Citation year selected by default: " + year);
            Logger.getLogger(CsvWosParser.class.getName()).log(Level.WARNING,
                    "Journal Citation year selected by default: " + year, e);

        }

        CSVParser parser = CSVFormat.RFC4180.withDelimiter(',').withIgnoreEmptyLines().withHeader()
                .parse(reader);

        for (CSVRecord csvRecord : parser) {
            if (!csvRecord.get("Rank").contains("Copyright")) {
                PubmedJournal journal = new PubmedJournal(
                        csvRecord.get("Full Journal Title").replaceAll("\\(.*?\\) ?", ""));
                if (csvRecord.isMapped("JCR Abbreviated Title")) {
                    journal.setJournalShortName(csvRecord.get("JCR Abbreviated Title"));
                }
                try {
                    journal.setYearImpactFactor(year, Float.parseFloat(csvRecord.get("Journal Impact Factor")));
                } catch (Exception e) {
                    System.out.println("Impact factor " + csvRecord.get("Journal Impact Factor") + " for "
                            + csvRecord.get("Full Journal Title"));
                    journal.setYearImpactFactor(year, -1);
                }
                this.journals.add(journal);
                this.currentBatchSize++;
                currentBatchSize++;
                if (this.currentBatchSize >= this.batchSize) {
                    currentBatchSize = 0;
                    for (PubmedJournal pm : journals) {
                        this.sqlM.updateJournal(pm, true);
                    }
                    this.journals = new ArrayList<>();
                }
            }

        }

        for (PubmedJournal pm : journals) {
            this.sqlM.updateJournal(pm, true);
        }
        this.journals = new ArrayList<>();

    } catch (FileNotFoundException ex) {
        Logger.getLogger(CsvWosParser.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(CsvWosParser.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SQLException ex) {
        Logger.getLogger(CsvWosParser.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:no.uio.medicine.virsurveillance.parserTests.SaxTest.java

private static ArrayList<String> getDataFromVirusCSV(String inputVirusFile)
        throws FileNotFoundException, IOException {
    ArrayList<String> viruses = new ArrayList<>();

    BufferedReader reader = new BufferedReader(new FileReader(inputVirusFile));
    CSVParser parser = CSVFormat.RFC4180.withDelimiter(';').withIgnoreEmptyLines().withHeader().parse(reader);
    for (CSVRecord csvRecord : parser) {
        if (csvRecord.isMapped("Virus Name")) {
            String currentVirus = csvRecord.get("Virus Name").replace(" virus", "").replace("", "%%");
            viruses.add(currentVirus);//from  ww w.  j  av a 2 s. co m
        }
    }

    return viruses;

}

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

/**
 * Loads the mapping file./*from  www  .  j ava 2 s. co  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

/**
 * Returns <code>true</code> if the given file can be parsed, <code>false</code> otherwise.
 *//*from w ww. ja v a2 s. c  o  m*/
public static boolean canImport(String filepath) throws IOException {
    checkArgument(new File(filepath).exists(), "The file must exist.");

    if (!CleanRatingsReader.canClean(filepath)) {
        return false;
    }

    try (CSVParser parser = createParser(filepath)) {
        for (CSVRecord record : parser) {
            // try to create just one rating
            return record.size() == RECORD_SIZE
                    && MynemoRating.isValid(TEST_USER, record.get(MOVIE_INDEX), record.get(VALUE_INDEX));
        }
    }

    // everything seems right
    return true;
}

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

/**
 * Creates a rating from a record. The record was usually created from a parser created by the
 * {@link #createParser(String)} method.
 *//*from w  w w .j  av  a2s.  c o  m*/
public static MynemoRating createRating(CSVRecord record) {
    return new MynemoRating(record.get(USER_INDEX), record.get(MOVIE_INDEX), record.get(VALUE_INDEX));
}