Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#90 opencsv doesn't work when lines contain quotes

open
nobody
None
5
2012-04-26
2012-04-26
Anonymous
No

When I run the program below, I expect to see 5 records parsed by opencsv. However, I only see 2. It seems opencsv has problems parsing the 2nd line (the badline) and then it silently ignores the remaining lines.

The problem I see is there are 3 quotes in the 2nd line. If they are removed, the program will parse 5 records.

I believe this is a bug. Can you take a look?

Thanks,
Jingjing

import java.io.StringReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import au.com.bytecode.opencsv.CSVReader;
import au.com.bytecode.opencsv.bean.CsvToBean;
import au.com.bytecode.opencsv.bean.HeaderColumnNameTranslateMappingStrategy;

public class OpenCsvTest {
public static void main(String[] args) {
String header = "name\taddress\temail\n";
String line1 = "john\t123 main st\tjohn@google.com\n";
String badline = "john\t123 mainst\tjxbnyqm%oz)/pathzewmtuaxvd/vjck@eqpzdzeq,*/@'%) s.comjf&',op):)\"danz!\"?8#\"@ wxg!.com_8towmwg.comrarp,(.s?\n";
System.out.println("badline: " + badline);
String line3 = "john\t123 main st\tjohn@google.com\n";
String line4 = "john\t123 main st\tjohn@google.com\n";
String line5 = "john\t123 main st\tjohn@google.com\n";

String lines = header + line1 + badline + line3 + line4 + line5;
readCsv(lines);
}

static List<RowBean> readCsv(String lines) {
CSVReader reader = new CSVReader(new StringReader(lines));

HeaderColumnNameTranslateMappingStrategy<RowBean> strat = new HeaderColumnNameTranslateMappingStrategy<RowBean>();
strat.setType(RowBean.class);
Map<String, String> map = new HashMap<String, String>();
map.put("name", "name");
map.put("address", "address");
map.put("email", "email");
strat.setColumnMapping(map);

CsvToBean<RowBean> csv = new CsvToBean<RowBean>();
List<RowBean> list = csv.parse(strat, reader);
System.out.println("Number of records parsed by CSVReader: " + list.size());

return list;
}
}

public class RowBean {
private String name;
private String address;
private String email;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}

Discussion