@Override public List<Object> deserialize(ByteBuffer ser) { try { String data = new String(Utils.toByteArray(ser), StandardCharsets.UTF_8); CSVParser parser = CSVParser.parse(data, CSVFormat.RFC4180); CSVRecord record = parser.getRecords().get(0); Preconditions.checkArgument(record.size() == fieldNames.size(), "Invalid schema"); ArrayList<Object> list = new ArrayList<>(fieldNames.size()); for (int i = 0; i < record.size(); i++) { list.add(record.get(i)); } return list; } catch (IOException e) { throw new RuntimeException(e); } }
public static String[] parseAsCsv(String key, String value, Function<String, String> valueProcessor) { String cleanValue = MultivalueProperty.trimFieldsAndRemoveEmptyFields(value); List<String> result = new ArrayList<>(); try (CSVParser csvParser = CSVFormat.RFC4180 .withHeader((String) null) .withIgnoreEmptyLines() .withIgnoreSurroundingSpaces() .parse(new StringReader(cleanValue))) { List<CSVRecord> records = csvParser.getRecords(); if (records.isEmpty()) { return ArrayUtils.EMPTY_STRING_ARRAY; } processRecords(result, records, valueProcessor); return result.toArray(new String[result.size()]); } catch (IOException e) { throw new IllegalStateException("Property: '" + key + "' doesn't contain a valid CSV value: '" + value + "'", e); } }
for (CSVRecord record : parser.getRecords())
try { final List<CSVRecord> csvRecords = CSVParser.parse(recordCsvString, CSVFormat.DEFAULT) .getRecords();
private List<String[]> getRecords(String content, CSVFormat format) throws IOException { List<String[]> records = new ArrayList<>(); CSVParser parser = CSVParser.parse(content, format); for (CSVRecord record : parser.getRecords()) { String[] line = new String[record.size()]; for (int i = 0; i < line.length; i++) { line[i] = record.get(i); } records.add(line); } return records; } }
public synchronized List<Result> read() throws IOException { CSVParser parser = null; util.ensureBaseResultDirExists(); try { File file = new File(resultFileName); parser = CSVParser.parse(file, Charset.defaultCharset(), CSVFormat.DEFAULT); List<CSVRecord> records = parser.getRecords(); List<Result> results = new ArrayList<>(); String header = null; for (CSVRecord record : records) { // First record is the CSV Header if (record.getRecordNumber() == 1) { header = record.toString(); continue; } List<ResultValue> resultValues = new ArrayList<>(); for (String val : record.toString().split(PherfConstants.RESULT_FILE_DELIMETER)) { resultValues.add(new ResultValue(val)); } Result result = new Result(resultFileDetails, header, resultValues); results.add(result); } return results; } finally { parser.close(); } }
private String[] toFirstRecordValues(final String expected, final CSVFormat format) throws IOException { return CSVParser.parse(expected, format).getRecords().get(0).values(); }
private void doOneRandom(final CSVFormat format) throws Exception { final Random r = new Random(); final int nLines = r.nextInt(4) + 1; final int nCol = r.nextInt(3) + 1; // nLines=1;nCol=2; final String[][] lines = generateLines(nLines, nCol); final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, format)) { for (int i = 0; i < nLines; i++) { // for (int j=0; j<lines[i].length; j++) System.out.println("### VALUE=:" + printable(lines[i][j])); printer.printRecord((Object[]) lines[i]); } printer.flush(); } final String result = sw.toString(); // System.out.println("### :" + printable(result)); try (final CSVParser parser = CSVParser.parse(result, format)) { final List<CSVRecord> parseResult = parser.getRecords(); final String[][] expected = lines.clone(); for (int i = 0; i < expected.length; i++) { expected[i] = expectNulls(expected[i], format); } Utils.compare("Printer output :" + printable(result), expected, parseResult); } }
CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(FILE_HEADER_MAPPING); FileReader fileReader = new FileReader("file"); CSVParser csvFileParser = new CSVParser(fileReader, csvFileFormat); List csvRecords = csvFileParser.getRecords();
Reader in = new FileReader("File Name"); CSVParser parser = new CSVParser(in, CSVFormat.EXCEL); List<CSVRecord> csvRecords = parser.getRecords();
URL url = new URL("https://spotifycharts.com/api/?download=true&limit=50&country=us&recurrence=daily&date=latest&type=viral)"); URLConnection urlConnection = url.openConnection(); InputStreamReader urlStreamReader = new InputStreamReader(urlConnection.getInputStream()); CSVParser cvs = new CSVParser(urlStreamReader, CSVFormat.DEFAULT); cvs.getRecords().stream().forEach(System.out::println);
public static void main(String[] args) throws IOException { FileReader fileReader = null; CSVFormat csvFileFormat = CSVFormat.DEFAULT.withQuote(null); String fileName = "test.csv"; fileReader = new FileReader(fileName); CSVParser csvFileParser = new CSVParser(fileReader, csvFileFormat); List<CSVRecord> csvRecords = csvFileParser.getRecords(); for (CSVRecord csvRecord : csvRecords) { System.out.println(csvRecord); } csvFileParser.close(); }
public List<Tuple> read(InputStream is) throws IOException { CSVFormat format = CSVFormat.RFC4180.withHeader().withDelimiter(delim.charAt(0)); CSVParser csvParser = new CSVParser(new InputStreamReader(is), format); List<CSVRecord> records = csvParser.getRecords(); header = csvParser.getHeaderMap().entrySet().stream() .sorted((e1, e2) -> e1.getValue().compareTo(e2.getValue())) .map(Map.Entry::getKey).toArray(String[]::new); labelCol = labelCol == -1 ? records.get(0).size() - 1 : labelCol; List<Tuple> ds = records.stream().parallel().map(this::extractValuedFeat).collect(Collectors.toList()); return ds; }
@Override public List<TestScenarioResult> records() { try (Reader bufferedReader = new BufferedReader(new InputStreamReader(checkNotNull(getClass().getResourceAsStream(fileName), fileName + " could not be found")))) { return CSVFormat.DEFAULT .withHeader(headers) .withSkipHeaderRecord(true) .parse(bufferedReader) .getRecords().stream() .map(TestScenarioResult::createFromCSV).collect(Collectors.toList()); } catch (Exception e) { throw new RuntimeException(fileName + " could not be opened", e); } }
@Override public List<TestScenarioResult> records() { try (Reader bufferedReader = new BufferedReader(new InputStreamReader(checkNotNull(getClass().getResourceAsStream(fileName), fileName + " could not be found")))) { return CSVFormat.DEFAULT .withHeader(headers) .withSkipHeaderRecord(true) .parse(bufferedReader) .getRecords().stream() .map(TestScenarioResult::createFromCSV).collect(Collectors.toList()); } catch (Exception e) { throw new RuntimeException(fileName + " could not be opened", e); } }
private List<T> loadValues() { try { final String load = persistService.load(persistKey, "", Optional::of); Reader in = new StringReader(load); return new CSVParser(in, CSVFormat.DEFAULT).getRecords() .stream() .map(v -> v.get(0)) .map(deserializer) .collect(toList()); } catch (IOException e) { LOGGER.error("Cant read suggestion from persistence for key: " + persistKey); return new ArrayList<>(); } }
CSVParser parser = CSVParser.parse(file, Charset.forName("UTF-8"),CSVFormat.RFC4180.withFirstRecordAsHeader().withSkipHeaderRecord()); List<CSVRecord> records = parser.getRecords(); for (CSVRecord record : records) { System.out.println(record.get("Name")); System.out.println(record.get("Type")); System.out.println(record.get("Price")); }
public List<StringMap> records(String input) { try { CSVParser parser = CSVFormat.RFC4180 .withHeader(header.toArray(new String[]{})) .parse(new StringReader(input)); return parser.getRecords().stream() .map(record -> new StringMap(record.toMap())) .collect(Collectors.toList()); } catch (IOException e) { String message = String.format("OOPS I couldn't parse the csv line! '%s'", input); throw new RuntimeException(message); } } }
public static Map<String, LatLong> postcodeToLatLong(String datasetProvider, DownloadUtils downloadUtils) throws Exception { Map<String, LatLong> postcodeToCoordMap = new HashMap<>(); InputStreamReader postcodeIsr = new InputStreamReader(downloadUtils.fetchInputStream(new URL(POSTCODE_TO_COORDINATE_URL), datasetProvider, ".csv")); CSVParser csvFileParser = new CSVParser(postcodeIsr, CSVFormat.DEFAULT); Iterator<CSVRecord> iter = csvFileParser.getRecords().iterator(); CSVRecord header = iter.next(); while (iter.hasNext()) { CSVRecord record = iter.next(); postcodeToCoordMap.put(record.get(1), new LatLong(record.get(2), record.get(3))); } return postcodeToCoordMap; } }
CSVFormat csvFormat = CSVFormat.DEFAULT.withNullString(""); CSVParser csvParser = new CSVParser(fileReader, csvFormat); for (CSVRecord csvRecord : csvParser.getRecords()) { String values[] = {csvRecord.get(0), csvRecord.get(1), csvRecord.get(2)}; for (String value : values) { if (value != null) { // as soon a value is not-null we add the array // and exit the for-loop csvContentsList.add(values); break; } } }