Handling Quotes in a CSV file

A

Al G

Hi,

I am reading a CSV file with the code below. When I run into a " (quote
mark) in the file, I get a "malformed line" exception.

Can anyone point me to a way to handle this?

Thanks in advance,
Al G


assume Win XP/SP2, VS2005Pro/VB, SP1)

Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(filename)
MyReader.TextFieldType = FileIO.FieldType.Delimited
MyReader.SetDelimiters(",")
While Not MyReader.EndOfData

Try

currentRow = MyReader.ReadFields()

field1 = currentRow(0)

field2 = currentRow(1)

etc...



Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException

MessageBox.Show("Malformed Line exception")

End Try

End While

End Using
 
P

PlatinumBay

I am unable to reproduce the error. I tested this using the following line
in a text file:

This,Is,A,This is a "Test"

Code:

Private Sub LoadCommaDelimetedTextFileIntoListBox() 'ByVal filePath As
String)
' Declare a variable named theTextFieldParser of type
TextFieldParser.
Dim theTextFieldParser As TextFieldParser

' Call the My feature's OpenTextFieldParser method passing in a file
path.
' Assign the resulting TxtFileParser object to theTextFieldParser
variable.
theTextFieldParser =
My.Computer.FileSystem.OpenTextFieldParser("TestFile.txt") 'filePath)

' Set TextFieldParser object's TextFieldType property to Delimited.
theTextFieldParser.TextFieldType =
Microsoft.VisualBasic.FileIO.FieldType.Delimited

' Configure delimiters to handle a comma delimited text file:
' Set TextFieldParser object's Delimiter's property to a string
array
' containing one element with the value ",".
theTextFieldParser.Delimiters = New String() {","}

' Declare a variable named currentRow of type string array.
Dim currentRow() As String

' While the end of file has not been reached....
While Not theTextFieldParser.EndOfData
Try
' Read the fields on the current line
' and assign them to the currentRow array variable.
currentRow = theTextFieldParser.ReadFields()

' Declare a variable named currentField of type String.
Dim currentField As String

' Use the currentField variable to loop
' through fields in the currentRow.
For Each currentField In currentRow
' Add the the currentField (a string)
' to the demoLstBox items.
Console.Write(currentField & " | ")
Next
Console.WriteLine()
Catch malFormLineEx As
Microsoft.VisualBasic.FileIO.MalformedLineException
Console.WriteLine("Line " & malFormLineEx.Message & "is not
valid and will be skipped.", "Malformed Line Exception")
Catch ex As Exception
Console.WriteLine(ex.Message & " exception has occurred.",
"Exception")
Finally
' If successful or if an exception is thrown,
' close the TextFieldParser.
theTextFieldParser.Close()
End Try
End While
End Sub

Code from http://blogs.vbcity.com/mcintyre/archive/2006/10/02/6338.aspx

Hope this helps,

Steve
 

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

Similar Threads


Top