|
|
Reading Text Files w/commas in the data
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
|
|
0
|
|
|
|
Reply
|
tk
|
5/5/2010 4:11:17 PM |
|
tk wrote:
>
> 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.
|
|
0
|
|
|
|
Reply
|
Mr
|
5/5/2010 7:59:39 PM
|
|
try using
string delieter = "\",";
as the delimiter.
Rich
*** Sent via Developersdex http://www.developersdex.com ***
|
|
0
|
|
|
|
Reply
|
Rich
|
5/5/2010 8:52:19 PM
|
|
"tk" <tkelley@hrimaging.com> wrote in message
news:ee6OY2G7KHA.4804@TK2MSFTNGP02.phx.gbl...
> 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?
Nope.
I really like this library:
http://www.codeproject.com/KB/database/GenericParser.aspx
|
|
0
|
|
|
|
Reply
|
Jeff
|
5/5/2010 8:53:20 PM
|
|
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[i] = T[i].ToString().Replace("\"", "");
}
...
}
This routine read the text file just like Excel.
Rich
*** Sent via Developersdex http://www.developersdex.com ***
|
|
0
|
|
|
|
Reply
|
Rich
|
5/5/2010 10:35:17 PM
|
|
On 05-05-2010 12:11, tk wrote:
> 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
|
|
0
|
|
|
|
Reply
|
ISO
|
5/6/2010 1:33:39 AM
|
|
|
5 Replies
359 Views
(page loaded in 0.082 seconds)
|
|
|
|
|
|
|
|
|