Removing the double quotes from a CSV file.

I would like to remove the double quotes from a CSV file but am having a bugger of time doing so.
   
My input file looks like this:

"Book1 number 1",120.00,140.00,,,"Great Book"
"Book2 number 1",12.00,99.99,,,"Losy Book"
"Book2 number 2",19.00,34.00,,,"Do not read"

First I break up the line into an array so as to be able to manipulate each field easily.

string[] strSplitData = strDataLine.Split(',');

I now have the line split into an array and can easily do whatever I need to do. But I do not require the " that is leading an trialing in array element 0 and array elment 5. So I try to remove the " with the replace function. The problem is that the replace function uses the " as a delimiter.



Here is the way to remove a , ( comma ) from the array elment.
strSplitData[0].Replace( ",", "" );

I've tried many different approaches but obviously not the right one.

Any Ideas   Idea

Thanks

PS Single quotes in the replace function cause a complier error.



Answer this question

Removing the double quotes from a CSV file.

  • Evils Dark

    You can try to parse it with the FileHelpers library

      http://filehelpers.sourceforge.net

      you need to define a class for the records:

    [DelimitedRecord("\"")]
    public YourClass
    {
       [FiledQuoted()]
       public string First;
       [FiledQuoted()]
       public double Second..

      ....
    }

     and then use the library like this.

      FIleHelpersEngine engine = new FileHelperEngine(typeof(YourClass));

       // to read
       YourClass[] res = engine.ReadFile("csv.txt");

       // to write
       engine.WriteFile("csvout.txt")

    Best Regards


  • Avnish Kumar Sharma

    I think it is not a good idea to do a simple split by coma.  If for example a text field has a coma in the middle Also simple removing quotas is not a best solution because if the field has a quota then in CSV file it will be doubbled - you've got to remove doubles as well. You can say It is not your case - well it is about design quality. Tongue Tied

    I would suggest to process the string as character by character.

    HTH

    Slav.


  • Dan Mork

    strSplitData[0] = strSplitData[0].Replace("\"", "");

  • Hong

    Thank you, your solution works as well.


  • Wojteq

    Thanks to my friend here's how to solve the problem above.

    strDataline.Replace( @"""", "" );

    The above syntax will remove any occurences of the quotation mark ". The answer lays in the usage of the escape clause, the @. Any double occurence of a character allow for that char to be contained within the varable. In this case the " mark. The @ is a system escape character.

  • Removing the double quotes from a CSV file.