JoshClose / CsvHelper

Library to help reading and writing CSV files
http://joshclose.github.io/CsvHelper/
Other
4.72k stars 1.06k forks source link

Escape Double-Quotes #369

Closed teiles closed 9 years ago

teiles commented 9 years ago

Given following three fields: A, B, and C Hi, you are "Great"! 00000000-0000-0000-0000-000000000000

Expected CSV row: "A, B, and C","Hi, you are ""Great""",00000000-0000-0000-0000-000000000000

Actual CSV row: "A, B, and C","You are "Great"",00000000-0000-0000-0000-000000000000

CsvHelper does not escape the double-quote with doubled double-quotes.

The current code is:

if (shouldQuote)
{
    field = this.configuration.Quote + field + this.configuration.Quote;
}

Reference

https://tools.ietf.org/html/rfc4180

  7.  If double-quotes are used to enclose fields, then a double-quote
       appearing inside a field must be escaped by preceding it with
       another double quote.  For example:

       "aaa","b""bb","ccc"

Should this feature be part of CsvHelper? If so, I'll write it.

teiles commented 9 years ago

Added around 2.6.1, we were on 2.5

JoshClose commented 9 years ago

Everything is working as expected then?

teiles commented 9 years ago

Josh ---

Not working because I was using WriteField(field, shouldQuote)

See PR.

Thanks --- Ted

On Fri, Apr 3, 2015 at 10:27 AM, Josh Close notifications@github.com wrote:

Everything is working as expected then?

— Reply to this email directly or view it on GitHub https://github.com/JoshClose/CsvHelper/issues/369#issuecomment-89303390.

JoshClose commented 9 years ago

OK. I'll open it back up then. :)

stefanscript commented 9 years ago

Hi,

Great job with the CsvHelper. Just wanted to mention i did encounter this same issue for text with left double quotes and right double quotes (curly quotes).

cheers

JoshClose commented 9 years ago

Can you give an example on how to reproduce? Thanks.

stefanscript commented 9 years ago

Hi Josh,

Thank you for the answer. I have some text "19” alloy wheels .... " ( after 19 a curly quote) And I write it like this ... csv.WriteField(vehicle.Features, true); ...

stefanscript commented 9 years ago

Don't know if makes a difference but need to use latin 1 (i'm annoyed too :) ) Data comes from database

using (StreamWriter writer = new StreamWriter(File.Open(filelocation, FileMode.Create), Encoding.GetEncoding("ISO-8859-1"))) .... csv.Configuration.Encoding = Encoding.GetEncoding("iso-8859-1");

JoshClose commented 9 years ago

Oh, I see. Those aren't the same as the quote character. I think you could make it always quote to solve that. Configuration.QuoteAllFields = true;