Reading Text Files w/commas in the data

T

tk

I have a text file that I need to import into a datatable. It is comma
delimited (and the fields are enclosed in quotes). I use the following code
to read the file.

reader = new StreamReader(txtFilePath.Text.Trim());

string data = reader.ReadToEnd();


string linedelimeter = "\r\n";

string delieter = ",";

string[] rows = data.Split(linedelimeter.ToCharArray());

foreach (string r in rows)

{

string[] items = r.Split(delimeter.ToCharArray());

}


The problem is if one of the fields has a comma in it, the split function
seperates it into 2 fields, even thou the field is in quotes. Is there a
way to handle the comma in the field besides manually parsing each line?

Thanks,

Tim
 
M

Mr. Arnold

tk said:
The problem is if one of the fields has a comma in it, the split function
seperates it into 2 fields, even thou the field is in quotes. Is there a
way to handle the comma in the field if besides manually parsing each line?

No, I don't think so. The comma is the delimiter, and it doesn't matter
if it has quotes around it or not.
 
R

Rich P

I just tried this routine which worked fine for a comma delimited
textfile with double quotes for each field and commas within the double
quotes. It picked up the commas inside the double quoted field without
splitting the field

StreamReader SR;
string S;
string[] sx = new string[] { "\"," };
string[] T;

SR = File.OpenText(s1 + "\\sampledata_5.txt");

S = SR.ReadLine();
if (S != null)
{
T = S.Split(sx, StringSplitOptions.None);
for (int i = 0; i < T.Length; i++)
{
dr = T.ToString().Replace("\"", "");
}
...
}

This routine read the text file just like Excel.


Rich
 
A

Arne Vajhøj

I have a text file that I need to import into a datatable. It is comma
delimited (and the fields are enclosed in quotes). I use the following code
to read the file.

reader = new StreamReader(txtFilePath.Text.Trim());

string data = reader.ReadToEnd();


string linedelimeter = "\r\n";

string delieter = ",";

string[] rows = data.Split(linedelimeter.ToCharArray());

foreach (string r in rows)

{

string[] items = r.Split(delimeter.ToCharArray());

}


The problem is if one of the fields has a comma in it, the split function
seperates it into 2 fields, even thou the field is in quotes. Is there a
way to handle the comma in the field besides manually parsing each line?

You need something more advanced.

Manually parsing may be the simplest solution. It is not that
difficult and you get it exactly as you need it.

I do not think regex or the ODBC driver capable of reading CSV
will make the code more readable.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top