List of usage examples for org.apache.commons.csv CSVRecord getRecordNumber
public long getRecordNumber()
From source file:com.mycompany.match.gender.app.FirstNamesListModel.java
static HashMap<String, String> getNames() throws IOException { InputStream in = Thread.currentThread().getClass().getResourceAsStream("/data/malenames.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); Iterable<CSVRecord> records = readCsv(reader); for (CSVRecord record : records) { if (record.getRecordNumber() >= 0) { // Here true represents male String key = record.get(0).toLowerCase(); GenderNamesList.put(key, "male"); }/*w ww .j a va 2 s . co m*/ } in = Thread.currentThread().getClass().getResourceAsStream("/data/femalenames.txt"); reader = new BufferedReader(new InputStreamReader(in)); records = readCsv(reader); for (CSVRecord record : records) { if (record.getRecordNumber() >= 0) { // Here false represents female String key = record.get(0).toLowerCase(); if (!GenderNamesList.containsKey(key)) { GenderNamesList.put(key, "female"); } else { GenderNamesList.put(key, "either"); } } } return GenderNamesList; }
From source file:com.mycompany.match.gender.app.PersonModel.java
static HashMap<String, Person> getPersonsData() throws IOException { InputStream in = Thread.currentThread().getClass().getResourceAsStream("/data/name_email_sample.csv"); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); Iterable<CSVRecord> records = readCsv(reader); for (CSVRecord record : records) { Person person;/*ww w. ja v a2 s . c om*/ if (record.getRecordNumber() != 1) { if (record.size() > 1) { person = new Person(record); persons.put(record.get(9), person); } } } return persons; }
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; }/* 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:de.upb.wdqa.wdvd.geolocation.GeolocationDatabase.java
/** * Reads the csv file of the TagDownloader *//* w ww.j ava 2 s. c o m*/ public static void readFile(File file) { try { logger.info("Starting to read file of GeolocationDatabase ..."); BufferedReader reader = new BufferedReader(new InputStreamReader( new BZip2CompressorInputStream(new BufferedInputStream(new FileInputStream(file))), "UTF-8")); CSVParser parser = new CSVParser(reader, CSVFormat.RFC4180); for (CSVRecord csvRecord : parser) { parseRecord(csvRecord); if (csvRecord.getRecordNumber() % 1000000 == 0) { logger.info("Current Record: " + csvRecord.getRecordNumber()); } } parser.close(); logger.info("Finished"); } catch (Exception e) { logger.error("", e); } }
From source file:de.upb.wdqa.wdvd.revisiontags.TagDownloader.java
/** * Reads the csv file of the TagDownloader *//*from w w w .j a v a 2 s . c o m*/ public static void readFile(File file) { try { logger.info("Starting to read file of TagDownloader ..."); BufferedReader reader = new BufferedReader(new InputStreamReader( new BZip2CompressorInputStream(new BufferedInputStream(new FileInputStream(file))), "UTF-8")); CSVParser parser = new CSVParser(reader, CSVFormat.RFC4180); dataStore.connect(); for (CSVRecord csvRecord : parser) { parseRecord(csvRecord); if (csvRecord.getRecordNumber() % 1000000 == 0) { logger.info("Current Record: " + csvRecord.getRecordNumber()); } } dataStore.disconnect(); parser.close(); logger.info("Tag Distribution:\n" + FrequencyUtils.formatFrequency(tagDistribution)); logger.info("Finished"); } catch (Exception e) { logger.error("", e); } }
From source file:it.incipict.tool.profiles.util.ProfilesToolUtils.java
public static List<Survey> parseSurvey(File file) throws ProfilesToolException { List<Survey> surveys = new ArrayList<Survey>(); Iterable<CSVRecord> records; try {//from w w w . java2s . c o m Reader in = new FileReader(file.getCanonicalPath()); records = CSVFormat.RFC4180.parse(in); } catch (IOException e) { throw new ProfilesToolException("error while reading files.", e); } // loop all surveys in CSV line by line // note: a single CSV can contain more surveys for (CSVRecord record : records) { int line = (int) record.getRecordNumber(); // skip the first line because it simply contains the headers if (line > 1) { Survey survey = new Survey(); List<String> answers = new ArrayList<String>(); List<String> textualAnswers = new ArrayList<String>(); // in the Survey model we can define a "RECORD OFFSET" by which // the parser must start for (int i = survey.RECORD_OFFSET; i < record.size(); i++) { String r = record.get(i); // if the "r" is empty the user has not responded // skip to the next answers if (r.isEmpty()) { continue; } // if "r" isn't a numerical value and it is different than // ZERO_STRING (Ref. Survey model) // Note: the ZERO_STRING is defined in Survey Class. Its // value is Zero. if ((!r.matches("\\d*") && r.compareTo(survey.ZERO_STRING) != 0)) { // Otherwise the answer is added to a list of Strings. // this list will appended in queue. (*) String[] str = r.split(";"); for (String s : str) { textualAnswers.add("\"" + s + "\""); } } // if "r" is a numeric value, simply add it in the answers // list. if (r.compareTo(survey.ZERO_STRING) == 0) { answers.add("0"); } if (r.matches("\\d*")) { answers.add(r); } } // add the textual answers in queue (*) answers.addAll(textualAnswers); String fname = file.getName(); survey.setProfile(fname.substring(0, fname.lastIndexOf('.'))); survey.setAnswers(answers); surveys.add(survey); } } return (surveys != null) ? surveys : null; }
From source file:com.sojw.TableNamesFinderExecutor.java
/** * @param tableName/*from w w w .j ava2 s .c o m*/ * @param fileContentList * @return */ private static List<String> extractTableNameByRegex(final String tableName, final List<CSVRecord> fileContentList) { List<String> searchList = new ArrayList<String>(); searchList.add(CSV_SAVE_HEADER); for (CSVRecord record : fileContentList) { final String iteration = record.get(0).trim(); final String groupno = record.get(1).trim(); final String sqlStatement = record.get(3).trim(); final String exPerSec = record.get(4).trim(); final long recordNumber = record.getRecordNumber(); /* final Pattern updatePattern = Pattern.compile("(?i)UPDATE[^\\S]+(?i)(dbo." + tableName + "|" + tableName + ")[^\\S]+(?i)SET"); final Matcher updateMatcher = updatePattern.matcher(sqlStatement); if (updateMatcher.find()) { searchList.add(iteration + ", " + groupno + "," + tableName + "," + sqlStatement + "," + exPerSec); } final Pattern queryPattern = Pattern.compile("(?i)FROM[^\\S]+(?i)(dbo." + tableName + "|" + tableName + ")[^\\S]+"); final Matcher queryMatcher = queryPattern.matcher(sqlStatement); if (queryMatcher.find()) { searchList.add(iteration + ", " + groupno + "," + tableName + "," + sqlStatement + "," + exPerSec); } */ final Pattern queryPattern = Pattern .compile("(?i)UPDATE[^\\S]+(?i)(dbo." + tableName + "|" + tableName + ")[^\\S]+(?i)SET" + "|" + "(?i)FROM[^\\S]+(?i)(dbo." + tableName + "|" + tableName + ")[^\\S]+"); final Matcher queryMatcher = queryPattern.matcher(sqlStatement); if (queryMatcher.find()) { searchList.add(String.format(CSV_SAVE_FORMAT, iteration, groupno, tableName, sqlStatement, exPerSec, recordNumber)); } } return searchList; }
From source file:com.ibm.watson.catalyst.jumpqa.template.TemplateFactory.java
@Override public ITemplate readRecord(final CSVRecord aRecord) { logger.fine("Reading record #" + aRecord.getRecordNumber()); final String templateClass = aRecord.get("Template Class"); logger.finer("Template class is" + templateClass); final ITemplateRecordReader trr = readers.get(templateClass); if (trr == null) throw new RuntimeException("Template Class not found: " + templateClass); return trr.readRecord(aRecord); }
From source file:gradingfun.GradeParser.java
public void parse() { Map<String, Double> data = new HashMap<String, Double>(); for (CSVRecord csvRecord : this.parser) { if (csvRecord.getRecordNumber() > 1) { String lastName = csvRecord.get(0); String firstName = csvRecord.get(1); String score = csvRecord.get(6); Double dblScore = null; try { dblScore = new Double(score); } catch (NumberFormatException ex) { dblScore = 0.0;//w ww .ja v a 2s . c o m } data.put(firstName + " " + lastName, dblScore); } } this.parsedData = data; try { this.parser.close(); } catch (IOException ex) { Logger.getLogger(GradeParser.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:functions.LoadCSVdata.java
public void LoadFeeDataToJTable(JTable t, String path) { try {/* w w w . ja v a 2 s .c o m*/ file = new File(path); parser = CSVParser.parse(file, Charset.forName("UTF-8"), CSVFormat.DEFAULT); DefaultTableModel model = (DefaultTableModel) t.getModel(); model.setRowCount(0); for (CSVRecord c : parser) { if (c.getRecordNumber() == 1) continue; model.addRow( new Object[] { c.get(datatype.GlobalVariable.TYPE), c.get(datatype.GlobalVariable.AMOUNT), c.get(datatype.GlobalVariable.PAID_BY), c.get(datatype.GlobalVariable.PAYER) }); } } catch (Exception e) { System.out.println(e); } }