Skip to content

Instantly share code, notes, and snippets.

@DominicFinn
Created August 1, 2016 15:36
Show Gist options
  • Save DominicFinn/9d4975d19d2bfa4444d5a7731587129a to your computer and use it in GitHub Desktop.
Save DominicFinn/9d4975d19d2bfa4444d5a7731587129a to your computer and use it in GitHub Desktop.
Reading a CSV file with two columns when the second column is full of commas and formatting it so it's enclosed in Quotes as per the CSV spec
Imports System.IO
Imports System.Text
Imports Microsoft.VisualBasic.FileIO
Module Module1
Sub Main()
dim items = New List(Of string)
using parser = New TextFieldParser("filename", Encoding.UTF8)
parser.SetDelimiters(",")
'parser.HasFieldsEnclosedInQuotes -> this is what we wished it was
While Not parser.EndOfData
dim row = parser.ReadFields()
If row.Length > 0 then
dim start = """" & row(0) & ""","""
For i As Integer = 1 To row.Length - 1
start &= row(i)
Next
start &= """"
items.Add(start)
End If
End While
End Using
file.WriteAllLines("new filename", items, Encoding.UTF8)
End Sub
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment