Created
August 1, 2016 15:36
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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