Prev: Help with multi-dimensional arrays
Next: Urgent! Global and Very Successful company needs C# Developer!
From: tk on 5 May 2010 12:11 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
From: Mr. Arnold on 5 May 2010 15:59 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.
From: Rich P on 5 May 2010 16:52 try using string delieter = "\","; as the delimiter. Rich *** Sent via Developersdex http://www.developersdex.com ***
From: Jeff Johnson on 5 May 2010 16:53 "tk" <tkelley(a)hrimaging.com> wrote in message news:ee6OY2G7KHA.4804(a)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
From: Rich P on 5 May 2010 18:35 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 ***
|
Next
|
Last
Pages: 1 2 Prev: Help with multi-dimensional arrays Next: Urgent! Global and Very Successful company needs C# Developer! |