apache-commons-csv

Write CSV from List of HashMaps with Header using Apache Commons CSV

点点圈 提交于 2020-03-05 03:24:47
问题 I have to take an ArrayList of HashMap and create a CSV using Apache Commons CSV. However, it's not writing the values to the right headers. Is there an easy way to have the library automatically place the values to the right headers using the Enum? I don't want to manually assign it as I will be adding more columns. This is what it's producing: Here is what I have: Header.java public enum Header { FIRST_NAME(), LAST_NAME(), GENDER(); } Main public static void main(String[] args) { List<Map

Java - Write CSV File with Apache.commons.csv

泪湿孤枕 提交于 2020-01-23 06:23:20
问题 I'm using the apache.commons.csv library in Java. I'm reading a CSV file from a web page with this code: InputStream input = new URL(url).openStream(); Reader reader = new InputStreamReader(input, "UTF-8"); defaultParser = new CSVParser(reader, CSVFormat.DEFAULT); excelParser = new CSVParser(reader, CSVFormat.EXCEL.withHeader()); defaultParsedData = defaultParser.getRecords(); excelParsedData = excelParser.getRecords(); However, I can't find a method in this library to easily write this file

Java - Write CSV File with Apache.commons.csv

会有一股神秘感。 提交于 2020-01-23 06:22:25
问题 I'm using the apache.commons.csv library in Java. I'm reading a CSV file from a web page with this code: InputStream input = new URL(url).openStream(); Reader reader = new InputStreamReader(input, "UTF-8"); defaultParser = new CSVParser(reader, CSVFormat.DEFAULT); excelParser = new CSVParser(reader, CSVFormat.EXCEL.withHeader()); defaultParsedData = defaultParser.getRecords(); excelParsedData = excelParser.getRecords(); However, I can't find a method in this library to easily write this file

skip double quotes when reading csv file using apache commons csv

自闭症网瘾萝莉.ら 提交于 2019-12-24 07:26:31
问题 Reader in = new FileReader(dataFile); Iterable<CSVRecord> records = CSVFormat.RFC4180.withFirstRecordAsHeader().withIgnoreEmptyLines(true).withTrim().parse(in); // Reads the data in csv file until last row is encountered for (CSVRecord record : records) { String column1= record.get("column1"); Here the column1 value in csv file is something like "1234557. So whe I read the column it is fetched with the quotes at the start. Is there any way in Apache commons csv to skip those. Sample data from

Error Parsing due to CSV Differences Before/After Saving (Java w/ Apache Commons CSV)

对着背影说爱祢 提交于 2019-12-23 12:18:08
问题 I have a 37 column CSV file that I am parsing in Java with Apache Commons CSV 1.2. My setup code is as follows: //initialize FileReader object FileReader fileReader = new FileReader(file); //intialize CSVFormat object CSVFormat csvFileFormat = CSVFormat.DEFAULT.withHeader(FILE_HEADER_MAPPING); //initialize CSVParser object CSVParser csvFileParser = new CSVParser(fileReader, csvFileFormat); //Get a list of CSV file records List<CSVRecord> csvRecords = csvFileParser.getRecords(); // process

CSV parsing with Commons CSV - Quotes within quotes causing IOException

我的梦境 提交于 2019-12-19 07:48:47
问题 I am using Commons CSV to parse CSV content relating to TV shows. One of the shows has a show name which includes double quotes; 116,6,2,29 Sep 10,""JJ" (60 min)","http://www.tvmaze.com/episodes/4855/criminal-minds-6x02-jj" The showname is "JJ" (60 min) which is already in double quotes. This is throwing an IOException java.io.IOException: (line 1) invalid char between encapsulated token and delimiter. ArrayList<String> allElements = new ArrayList<String>(); CSVFormat csvFormat = CSVFormat

Apache commons CSV: quoted input doesn't work

流过昼夜 提交于 2019-12-11 08:15:29
问题 import java.io.IOException; import java.io.StringReader; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; I try to parse a simple csv file with Apache CSV parser. It works fine as long as I don't use quotes. When I try to add a quote to the input "a";42 it gives me the error: invalid char between encapsulated token and delimiter Here is a simple, complete code: public class Test { public static void main(String[] args) throws IOException { String DATA = "\"a\"

CSV parsing with Commons CSV - Quotes within quotes causing IOException

[亡魂溺海] 提交于 2019-12-01 06:16:28
I am using Commons CSV to parse CSV content relating to TV shows. One of the shows has a show name which includes double quotes; 116,6,2,29 Sep 10,""JJ" (60 min)"," http://www.tvmaze.com/episodes/4855/criminal-minds-6x02-jj " The showname is "JJ" (60 min) which is already in double quotes. This is throwing an IOException java.io.IOException: (line 1) invalid char between encapsulated token and delimiter. ArrayList<String> allElements = new ArrayList<String>(); CSVFormat csvFormat = CSVFormat.DEFAULT; CSVParser csvFileParser = new CSVParser(new StringReader(line), csvFormat); List<CSVRecord>

How can I process a large file via CSVParser?

眉间皱痕 提交于 2019-11-30 18:39:26
I have a large .csv file (about 300 MB), which is read from a remote host, and parsed into a target file, but I don't need to copy all the lines to the target file. While copying, I need to read each line from the source and if it passes some predicate, add the line to the target file. I suppose that Apache CSV ( apache.commons.csv ) can only parse whole file CSVFormat csvFileFormat = CSVFormat.EXCEL.withHeader(); CSVParser csvFileParser = new CSVParser("filePath", csvFileFormat); List<CSVRecord> csvRecords = csvFileParser.getRecords(); so I can't use BufferedReader . Based on my code, a new

How can I process a large file via CSVParser?

筅森魡賤 提交于 2019-11-30 02:57:23
问题 I have a large .csv file (about 300 MB), which is read from a remote host, and parsed into a target file, but I don't need to copy all the lines to the target file. While copying, I need to read each line from the source and if it passes some predicate, add the line to the target file. I suppose that Apache CSV ( apache.commons.csv ) can only parse whole file CSVFormat csvFileFormat = CSVFormat.EXCEL.withHeader(); CSVParser csvFileParser = new CSVParser("filePath", csvFileFormat); List