Find a delimiter of csv or text files in c# -


i want find delimiter being used separate columns in csv or text files.

i using textfieldparser class read files.

below code,

string path = @"c:\abc.csv"; datatable dt = new datatable(); if (file.exists(path)) {     using (microsoft.visualbasic.fileio.textfieldparser parser = new microsoft.visualbasic.fileio.textfieldparser(path))     {         parser.textfieldtype = fieldtype.delimited;         if (path.contains(".txt"))         {                    parser.setdelimiters("|");         }         else         {             parser.setdelimiters(",");         }         parser.hasfieldsenclosedinquotes = true;         bool firstline = true;         while (!parser.endofdata)         {             string[] fields = parser.readfields();             if (firstline)             {                   foreach (var val in fields)                   {                       dt.columns.add(val);                   }                   firstline = false;                   continue;              }              dt.rows.add(fields);           }      }  lblcount.text = "count of total rows in file: " + dt.rows.count.tostring();  dgvtextfieldparser1.datasource = dt; 

instead of passing delimiters manually based on file type, want read delimiter file , pass it.

how can that?

mathematically correct totally useless answer: it's not possible.

pragmatical answer: it's possible but depends on how know file's structure. boils down bunch of assumptions , depending on we'll make, answer vary. , if can't make assumptions, well... see mathematically correct answer.

for instance, can assume delimiter 1 or of elements in set below?

list<char> delimiters = new list<char>{' ', ';', '|'}; 

or can assume delimiter such produces elements of equal length?

should try find delimiter that's single character or can word one?

etc.

based on question, i'll assume it's first option , have limited set of possible characters, precisely 1 of delimiter given file.

how count number of occurrences of each such character , assume 1 that's occurring one? sufficiently rigid or need more sure that?

list<char> delimiters = new list<char>{' ', ';', '-'}; dictionary<char, int> counts = delimiters.todictionary(key => key, value => 0); foreach(char c in delimiters)   counts[c] = textarray.count(t => t == c); 

i'm not in front of computer can't verify last step returning key dictionary value of maximal.

you'll need take consideration special case such there's no delimiters detected, there equally many delimiters of 2 types etc.


Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -