0

I have a CSV file that all fields have quoted text. My problem is that I have quotes within quotes as follows.

"John Doe","I have a 17" screen","Something "A" something else",....

I have tried Microsoft.VisualBasic.FileIO and LumenWorks.Framework.IO.Csv and both are not allowing quotes within those fields.

Jonathan
  • 651
  • 8
  • 14
  • 1
    The embedded should be doubled, e.g. `"I have a 17"" screen"` to be properly formatted, which is why the libraries are having trouble parsing it. If you don't control the source you will have to parse the string manually. – shf301 Jan 31 '13 at 20:44

3 Answers3

2

If you are don't have an option to do proper double quotes in the source files and need to parse this manually, this might help you:

var split = theCsv.Split(',');
var noQuotesCollection = split.Select(p =>
{
    if (p.StartsWith("\"") && p.EndsWith("\"") && p.Length > 1)
        return p.Substring(1, p.Length - 2);
    return p;
});
Artless
  • 4,522
  • 1
  • 25
  • 40
1

In order to have quotes in a CSV file you have to use double quotes.

Here is a sample

"here is some text","here is some more text","here is some text with a quotation, ""Hello World"""

cepatt
  • 726
  • 11
  • 23
0

Have you tried splitting the string on "," ? Assuming that's consistent throughout your file that should work. Of course, if you have an internal "," within a field you're out of luck.

Check this SO thread for an example of splitting on multiple characters.

Community
  • 1
  • 1