public static void main(String[] args) throws FileNotFoundException { // 1st, config the CSV reader CsvParserSettings settings = new CsvParserSettings(); settings.getFormat().setLineSeparator("\n"); settings.getFormat().setQuote('\''); // set the quote to single quote ' settings.getFormat().setQuoteEscape('\\'); // escape the double quote " // 2nd, creates a CSV parser with the configs CsvParser parser = new CsvParser(settings); // 3rd, parses all rows from the CSV file into a 2-dimensional array List<String[]> resolvedData = parser.parseAll(new StringReader("1,mm/dd/yy,'abc,def,\"pqr\",xyz'")); for (String[] row : resolvedData) { StringBuilder strBuilder = new StringBuilder(); for (String col : row) { strBuilder.append(col).append("\t"); } System.out.println(strBuilder); } }
//initialize a parser for the pipe separated bit CsvParserSettings detailSettings = new CsvParserSettings(); detailSettings.getFormat().setDelimiter('='); detailSettings.getFormat().setLineSeparator("|"); CsvParser detailParser = new CsvParser(detailSettings); //here is the content of the last column (assuming you got it from the parser) String details = "destination_ip=192.62.3.74|product_id=0071|option1_type=(s-dns)|proxy_machine_ip=10.1.255.3"; //The result will be a list of pairs List<String[]> pairs = detailParser.parseAll(new StringReader(details)); //You can add the pairs to a map Map<String, String> map = new HashMap<String, String>(); for (String[] pair : pairs) { map.put(pair[0], pair[1]); } //this should print: {destination_ip=192.62.3.74, product_id=0071, proxy_machine_ip=10.1.255.3, option1_type=(s-dns)} System.out.println(map);
import java.lang.String; import com.univocity.parsers.csv.CsvParser; import com.univocity.parsers.csv.CsvParserSettings; public class StamMain { public static void main(String[] args){ String str = "\"value1,value2\",value3"; System.out.println(str); CsvParserSettings settings = new CsvParserSettings(); settings.getFormat().setQuote('"'); CsvParser parser = new CsvParser(settings); String[] fields = parser.parseLine(str); for (String f : fields) System.out.println(f); } }
File initialFile = new File("/Users/me/Downloads/file.csv"); InputStream targetStream = FileUtils.openInputStream(initialFile); CsvParserSettings settings = new CsvParserSettings(); settings.getFormat().setLineSeparator("\n"); CsvParser parser = new CsvParser(settings); List<String[]> allRows = parser.parseAll(new InputStreamReader(targetStream, "UTF-8"));
CsvParserSettings settings = new CsvParserSettings(); //many options here, check the tutorial settings.getFormat().setComment('~'); // creates a CSV parser CsvParser parser = new CsvParser(settings); // parses all rows in one go. List<String[]> allRows = parser.parseAll(new FileReader(new File("/path/to/file.csv")));
List<String[]> resolvedData; String[][] array; public void parseURLs() throws FileNotFoundException { CsvParserSettings settings = new CsvParserSettings(); settings.getFormat().setLineSeparator("\n"); CsvParser parser = new CsvParser(settings); try { resolvedData = parser.parseAll(new FileReader("C:\\Users\\kevin.anderson\\Desktop\\book1.csv")); array = new String[resolvedData.size()][]; resolvedData.toArray(array); } catch (Exception ex) { System.out.println(ex.toString()); } }
CsvParserSettings settings = new CsvParserSettings(); // many options here, check the tutorial. settings.getFormat().setLineSeparator("\r\n"); CsvParser parser = new CsvParser(settings); List<String[]> allRows = parser.parseAll(new FileReader(new File("path/to/file.csv")));
CsvParserSettings settings = new CsvParserSettings(); //many options here, check the tutorial settings.getFormat().setLineSeparator("\r\n"); settings.getFormat().setNormalizedNewline('\u0001'); //uses a special character to represent a new record instead of \n. settings.setNormalizeLineEndingsWithinQuotes(false); //does not replace \r\n by the normalized new line when reading quoted values. settings.setHeaderExtractionEnabled(true); //extract headers from file settings.trimValues(false); //does not remove whitespaces around values CsvParser parser = new CsvParser(settings); List<Record> recordsList = parser.parseAllRecords(new File("201404051539.csv"));
CsvParserSettings settings = new CsvParserSettings(); settings.getFormat().setLineSeparator("\n"); // creates a CSV parser CsvParser parser = new CsvParser(settings); // parses all rows in one go. List<String[]> allRows = parser.parseAll(new File("C:"+File.separator+"followers.csv"))); Set<String> result = new LinkedHashSet<>(); for(String[] row : allRows){ for(String element : row){ if(element != null){ //assuming the case of these these elements don't matter //remove the ".toLowerCase()" part if it does. result.add(element.toLowerCase()); } } } System.out.println(result); //here's your deduplicated data.
/** * Builds a new {@code CsvFormatDetector} * * @param maxRowSamples the number of row samples to collect before analyzing the statistics * @param settings the configuration provided by the user with potential defaults in case the detection is unable to discover the proper column delimiter or quote character. * @param whitespaceRangeStart starting range of characters considered to be whitespace. */ CsvFormatDetector(int maxRowSamples, CsvParserSettings settings, int whitespaceRangeStart) { this.MAX_ROW_SAMPLES = maxRowSamples; this.whitespaceRangeStart = whitespaceRangeStart; allowedDelimiters = settings.getDelimitersForDetection(); if (allowedDelimiters != null && allowedDelimiters.length > 0) { suggestedDelimiter = allowedDelimiters[0]; } else { String delimiter = settings.getFormat().getDelimiterString(); suggestedDelimiter = delimiter.length() > 1 ? ',' : settings.getFormat().getDelimiter(); allowedDelimiters = new char[0]; } normalizedNewLine = settings.getFormat().getNormalizedNewline(); comment = settings.getFormat().getComment(); }
public static void main(String[] args) throws FileNotFoundException { // 1st, config the CSV reader CsvParserSettings settings = new CsvParserSettings(); settings.getFormat().setLineSeparator("\n"); settings.selectFields("Color1", "Color3", "Color2"); // 2nd, creates a CSV parser with the configs CsvParser parser = new CsvParser(settings); // 3rd, parses all rows of data in selected columns from the CSV file into a matrix List<String[]> resolvedData = parser.parseAll(new FileReader("/examples/example.csv")); // 3rd, process the matrix with business logic for (String[] row : resolvedData) { StringBuilder strBuilder = new StringBuilder(); for (String col : row) { strBuilder.append(col).append("\t"); } System.out.println(strBuilder); } }
CsvParserSettings parserSettings = new CsvParserSettings(); parserSettings.getFormat().setLineSeparator("\n"); parserSettings.getFormat().setDelimiter('$'); parserSettings.setHeaderExtractionEnabled(true); // To get the values of all columns, use a column processor ColumnProcessor rowProcessor = new ColumnProcessor(); parserSettings.setRowProcessor(rowProcessor); CsvParser parser = new CsvParser(parserSettings); //This will kick in our column processor parser.parse(new FileReader("testing.cvs")); //Finally, we can get the column values: Map<String, List<String>> columnValues = rowProcessor.getColumnValuesAsMapOfNames();
CsvParserSettings settings = new CsvParserSettings(); settings.setLineSeparatorDetectionEnabled(true); settings.getFormat().setDelimiter('|'); CsvParser parser = new CsvParser(settings); List<String[]> allLines = parser.parseAll(YOUR_INPUT_HERE);
public void parseCsvToBeanWithList() { final BeanListProcessor<Client> clientProcessor = new BeanListProcessor<Client>(Client.class); CsvParserSettings settings = new CsvParserSettings(); settings.getFormat().setLineSeparator("\n"); settings.setRowProcessor(clientProcessor); CsvParser parser = new CsvParser(settings); parser.parse(new StringReader(CSV_INPUT)); List<Client> rows = clientProcessor.getBeans(); }
@Test public void fieldSelectionTest() throws Exception { CsvParserSettings settings = new CsvParserSettings(); settings.setHeaderExtractionEnabled(true); settings.getFormat().setLineSeparator("\n"); settings.setNullValue("N/A"); //null value here settings.selectFields("Year", "???"); CsvParser parser = new CsvParser(settings); String input = "" + "Year,Header1,Header2\n" + "2000,foo,bar\n" + "2016,blah,etc\n"; for(String[] row : parser.parseAll(new StringReader(input))){ System.out.println(Arrays.toString(row)); } }
public static void main(String[] args) throws IOException{ //creates a processor of java beans. BeanListProcessor<UserAttributeRecord> beanProcessor = new BeanListProcessor<UserAttributeRecord>(UserAttributeRecord.class); //then a settings object to configure the parser CsvParserSettings settings = new CsvParserSettings(); //many options here, check the tutorial //configures the parser to use the bean processor. settings.setRowProcessor(beanProcessor); //configures the input format. settings.setHeaders("userId", "attributeId", "rating", "timestamp", "email", "attributeDesc"); settings.getFormat().setLineSeparator("\n"); //creates a parser with your settings CsvParser parser = new CsvParser(settings); //parses everything. All rows are submitted to the row processor defined above parser.parseAll(new FileReader(new File("/path/to/file.csv"))); //here's your list of beans List<UserAttributeRecord> beans = beanProcessor.getBeans(); }
/** * Provides a basic CSV configuration that allows parsing CSV files produced by Microsoft Excel. * * @return a pre-configured {@link CsvParserSettings} object with suggested settings * for parsing CSV files produced by Microsoft Excel. */ public static CsvParserSettings parseExcel() { CsvParserSettings settings = new CsvParserSettings(); settings.getFormat().setLineSeparator("\r\n"); settings.getFormat().setComment('\0'); settings.setParseUnescapedQuotes(false); settings.setSkipEmptyLines(false); settings.trimValues(false); return settings; }
public <T> List<T> getRows(String lineSep, String delimiter, CsvRowMapper<T> rowMapper) { CsvParserSettings settings = new CsvParserSettings(); settings.getFormat().setLineSeparator(lineSep); settings.getFormat().setDelimiter(delimiter.charAt(0)); RowListProcessor rowProcessor = new RowListProcessor(); settings.setProcessor(rowProcessor); CsvParser parser = new CsvParser(settings); parser.beginParsing(targetFile); List<T> rows = new ArrayList<>(); String[] row; int rowNumber = 1; while ((row = parser.parseNext()) != null) { T mappedRow = rowMapper.mapRow(rowNumber, row); if(mappedRow != null) { rows.add(rowMapper.mapRow(rowNumber, row)); } rowNumber++; } parser.stopParsing(); return rows; }
private void init(String filename) { CsvParserSettings settings = new CsvParserSettings(); settings.getFormat().setLineSeparator("\n"); CsvParser csvParser = new CsvParser(settings); csvParser.beginParsing(getReader(filename)); this.parser = csvParser; }