添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am reading a csv file using opencsv.

I am ignoring the first line of; the csv file is tab separated with some values enclosed in double quotes.

The problem occurs when I read the values of a column that has the '\' character, this is stripped out of the value.

reader = new CSVReader(new FileReader(exchFileObj),'\t','"',1);

For example in original file:

address = 12\91buenosaires   

It becomes as:

address = 1291buenosiares

In the string array that csvreader generates. How do I modify it to be able to read the '\' character also?

If opencsv uses the \` as escape character, you might need to enter \\\` in your file. If you cannot, can you set a different escape character in open csv? – rsp May 15, 2011 at 12:42

I had the same problem and couldn't find another character I could guarantee wouldn't show up in my csv file. According to a post on sourceforge though, you can use the explicit constructor with a '\0' to indicate that you don't want any escape character.

http://sourceforge.net/tracker/?func=detail&aid=2983890&group_id=148905&atid=773542

CSVParser parser = new CSVParser(CSVParser.DEFAULT_SEPARATOR, CSVParser.DEFAULT_QUOTE_CHARACTER, '\0', CSVParser.DEFAULT_STRICT_QUOTES);

I did a bit of cursory testing, and this seems to work just fine, at least backslashes certainly make it through.

i am using kotlin, and passing '\0' to CSVWriter as escape character gives invalid escape character error. Is there any other way to avoid the escape character? My work around is using ' ', which is working but it adds an unnecessary space – Bosco Aug 19, 2022 at 1:03 What if I want my CSV to support any Unicode character? Sounds like just a workaround to a bug in the library. – Mihai Danila Aug 2, 2019 at 15:15 The link is now broken. I could not find any constructor in which you can set the escape character - opencsv.sourceforge.net/apidocs/com/opencsv/CSVReader.html – Ravindra Gullapalli Nov 17, 2020 at 10:26 @RavindraGullapalli from version 5 the idea is to use the builder pattern, see opencsv.sourceforge.net/#configuration – rsp Nov 17, 2020 at 15:18

Note: I think the solution in this answer is better than the three alternatives in that it configures a compliant reader in a coarse-grained manner, by relying on the RFC. The other answers go into the details of configuring an escape character. While that works, it seems more like a white-box solution.

By default, OpenCSV's reader does not comply with the writer. The reader is not RFC-compliant. Don't ask me why that is, as I find it as troubling and perplexing as you.

The solution is for you to configure your CSVReader with an RFC-compliant parser:

RFC4180Parser rfc4180Parser = new RFC4180ParserBuilder().build();
CSVReaderBuilder csvReaderBuilder =
  new CSVReaderBuilder(new StringReader(writer.toString()))
      .withCSVParser(rfc4180Parser);
reader = csvReaderBuilder.build();

Here is the source page for the above.

In addition to @JMM 's answer, you have to use this created CSVParser in the constructor of the CSVReader. The only available constructor is:

public CSVReader(Reader reader, int line, CSVParser csvParser)

You can set the line to 0 so that it will not skip anything

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.