Feb
22
Written by:
Steve Gray
2/22/2010 3:45 PM
Let me tell you, this was a pain. It took me hours to figure this out.
This is a ground breaking piece of code. It uses a regex to correctly split a line of data – embedded commas are handled.
Here is how to read a .CSV file using Linq:
Dim lines As String() = System.IO.File.ReadAllLines(strCustomerFile)
Dim pattern As String = ",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))"
Dim r As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(pattern)
Dim custs = From line In lines _
Let data = r.Split(line) _
Select New With {.custnmbr = data(0), _
.custname = data(1)}
For Each cust In custs
strCUSTNMBR = Replace(cust.custnmbr, Chr(34), "")
strCUSTNAME = Replace(cust.custname, Chr(34), "")
Next
2 comment(s) so far...
Re: Reading a CSV file using Linq
Damn thank you soo much i been tryin to get this sorted out for awhile LOL I am in learning phase soo believe me its better to spend hours coding than tryin to figure how what the codes are doing ahahaha
Anyways thank you soooo much for putting it together :)
By Megatron on
5/31/2010 7:50 AM
|
Re: Reading a CSV file using Linq
Grats on creating this excellent regex!
By Josh on
8/25/2010 3:50 AM
|
As always, I welcome your comments!