[RESOLVED] Receiving quotation marks from a .CSV file???
I'm getting some weird input in my VB.Net 2005 program. I am reading a .CSV file as input and parsing the fields. When I look at the raw file, there are no quotation marks in the file, however, when I parse an Address field, it places quotation marks around the value and I'm not sure why it's doing that. It does it for 3 other fields that basically contain city, state, zip. None of these values in the .CSV file have quotes around them. Here is the code I use to retrieve the data.
Code:
Dim arrLine(16) As String
F1 = System.IO.File.OpenText(Trim(txtTransmissionFileName.Text))
Do While Not F1.EndOfStream
strLine = F1.ReadLine
arrLine = Split(strLine, ",")
stcInput.custNO = Trim(arrLine(0))
stcInput.custLocNO = Trim(arrLine(1))
stcInput.invoiceNO = Trim(arrLine(2))
stcInput.invoiceDate = Trim(arrLine(3))
stcInput.truckID = Trim(arrLine(4))
stcInput.transDateTime = Trim(arrLine(5))
stcInput.custLocAddr = Trim(arrLine(6))
stcInput.custLocCity = Trim(arrLine(7))
stcInput.custLocState = Trim(arrLine(8))
stcInput.custLocZip = Trim(arrLine(9))
stcInput.prodDesc = Trim(arrLine(10))
stcInput.gallons = Trim(arrLine(11))
stcInput.prodPrice = Trim(arrLine(12))
stcInput.taxPerGallon = Trim(arrLine(13))
stcInput.transTotAmt = Trim(arrLine(14))
stcInput.fedExciseTax = Trim(arrLine(15))
stcInput.stateExciseTax = Trim(arrLine(16))
stcInput.miscTaxes = Trim(arrLine(17))
stcInput.salesTax = Trim(arrLine(18))
stcInput.prodCD = Trim(arrLine(19))
Loop
The lines in red are inserting quotation marks. Here is what the data looks like:
""12460 MAR VISTA DRIVE""
""DAYTON""
""OH""
""CLR CARB ULS #2""
Any ideas what is causing this?
Thanks,
Re: Receiving quotation marks from a .CSV file???
are you sure the data doesn't have quotes? What program have you viewed it in?
If you open it in excel, which is often the default program associated with CSV files, excel does some parsing for you to put the data in columns. If you open the file in notepad, you might see that it does infact have quotes.
Also maybe you should try using a nice little class called the TextFieldParser class, which takes a bunch of the manual parsing labor out of your hands...
Code:
Dim MyParser As New Microsoft.VisualBasic.FileIO.TextFieldParser("CSV File here")
MyParser.Delimiters = New String() {","}
MyParser.HasFieldsEnclosedInQuotes = True
Dim MyData() As String = Nothing
Do Until MyParser.EndOfData
MyData = MyParser.ReadFields
'DO WHATEVER U NEED TO DO WITH DATA HERE
Loop
MyParser.Close()
MyParser.Dispose()
Re: Receiving quotation marks from a .CSV file???
Actually Kleinma, I was viewing the input file, *.CSV, in Excel, but then I opened it up in Word Pad and saw the Quotes. So, I will try your TextFieldParser class and see what happens.
As an alternative though, if I were to use the Replace function, how would I replace Quotation Marks since the string would have to be enclosed in Quotes?
ex: Replace(strVar,""", "")
The above syntax doesn't work?
Thanks!
Re: Receiving quotation marks from a .CSV file???
when you need to use a quote IN a string in the IDE you need to double them up, so even though it looks odd, you use 4 quote chars
ex: Replace(strVar,"""", "")
however parsing this way is bad because if any text field in the CSV file has a legit " char in it, you will remove it....
using the parser class won't give you that headache.
Re: Receiving quotation marks from a .CSV file???
I'm not sure how to use that Parser Class. How would I strip the quotes from the data? Because I would probably use the Replace function because of it's speed.
Re: Receiving quotation marks from a .CSV file???
Quote:
Originally Posted by blakemckenna
I'm not sure how to use that Parser Class. How would I strip the quotes from the data? Because I would probably use the Replace function because of it's speed.
Setting "MyParser.HasFieldsEnclosedInQuotes = True" takes care of the quotes automagically for you. To your code, it's like they were never there. You never have to deal with them at all.
Re: Receiving quotation marks from a .CSV file???
Got it!!!
works great!
Thanks!