0

I have a CSV file that contains several lines like this one :

"bar","foo, bar","18","07/09/2012 02:08:16","payments, recent","payments, all"

Some values ​​contain commas, and I need to remove these commas in order to obtain this result :

"bar","foo bar","18","07/09/2012 02:08:16","payments recent","payments all"

I started with this regex "^(\".+\"\\,?)+$" but it becomes too much complicated for me.


The final goal is to split that string :

string content = reader.ReadToEnd();

string[] lignes = contenu.Split(new[] { Environment.NewLine }, StringSplitOptions.None);

for (int i = 1; i < lignes.Length; i++)
{
    // REMOVE COMMAS

    string[] values = csv.Split(new[] {','});

    // do something
}

reader.Close();

Thanks.

dbc
  • 104,963
  • 20
  • 228
  • 340
Anthony Simmon
  • 1,579
  • 12
  • 26

4 Answers4

6

Instead of hand parsing your valid CSV file (commas are allowed within quoted fields), you should be using a CSV parser that knows how to handle these.

A popular library is FileHelpers and there is the TextFieldParser in the Microsoft.VisualBasic.FileIO namespace.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
1

Use the TextFieldParser class. It can handle commmas inside quotation marks.

http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.aspx

Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48
1

You could do some simple massaging of the data first, something like this maybe:

   string content = "\"bar\",\"foo, bar\",\"18\",\"07/09/2012 02:08:16\",\"payments, recent\",\"payments, all\"";

    content = content.Replace("\",\"", "~");
    content = content.Replace(",", ""); // Safe to remove commas now.
    content = content.Replace("\"", ""); // Get rid of left over double quotes.

    string[] values = content.Split(new[] { '~' });
Todd Bellamy
  • 152
  • 3
0

You can use this bellow please ignore method name and import file to your table

  private void ImportCSV(string filePath = @"E:\nucc_taxonomy_140.csv", string tableName = "TempTaxonomyCodes")
    {
        string tempPath = System.IO.Path.GetDirectoryName(filePath);
        string strConn = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + tempPath + @"\;Extensions=asc,csv,tab,txt";
        OdbcConnection conn = new OdbcConnection(strConn);
        OdbcDataAdapter da = new OdbcDataAdapter("Select * from " + System.IO.Path.GetFileName(filePath), conn);
        DataTable dt = new DataTable();
        da.Fill(dt);

        using (SqlBulkCopy bulkCopy = new SqlBulkCopy(ConfigurationSettings.AppSettings["dbConnectionString"]))
        {
            bulkCopy.DestinationTableName = tableName;
            bulkCopy.BatchSize = 50;
            bulkCopy.WriteToServer(dt);
        }

    }
Vishal Sen
  • 1,135
  • 1
  • 13
  • 23