Read File .txt separated by comma


For help I need to pass these lena separated by comma to a datatable.

these are the lines. at the end of the line also the separation is one per comma.

000000000000056680640,07/07/2016,1606169715409,000000933.00,DB,EXI IB INFOTEP 501606169715409  RD$.00,409,0000000000000,
000000000000056680640,08/07/2016,0000000000004,000000044.02,DB,PAGO IMPUESTO 0.15  DGII 4 TRANS POR $ 29348.29 Del 01/07/2016 Al 07/07/2016,524,0000000000000,
000000000000056680640,11/07/2016,0000777517509,000007000.00,CR,IB MB desde 777517509 ,201,0000000000000,
000000000000056680640,13/07/2016,0000120000002,000050490.00,CR,DEPOSITO CHEQUE Y EFECTIVO,108,0000120000002,
000000000000056680640,13/07/2016,0160438491157,000008564.40,DB,EXI IB DGII 160438491157  RD$.00,409,0000000000000,
000000000000056680640,13/07/2016,0000000000000,000000075.00,DB,COMISION PAGO IMPUESTO X IB  RD$.00,524,0000000000000,
000000000000056680640,13/07/2016,0003579084854,000001851.27,DB,EXI IB DGII 3579084854  RD$.00,409,0000000000000,
asked by JOSE ANGEL RAMIREZ HERNANDEZ 20.08.2016 в 00:00

2 answers


There is usually a problem with the files separated by commas , and it is a format that is not standardized, and different people (and systems) assume different things in the details.

In the words of the article Comma-separated values of the Wikipedia in English , freely translated:


The CSV file format is not standardized. The basic idea of separating fields with a comma is clear, but that idea is complicated when the data of the fields in turn contain commas or, even, line terminations.


Different CSV implementations may not support such data in the fields, or they may use quotes to enclose a field. Quotation marks do not solve everything: Some fields may also contain quotes, so certain CSV implementations might include characters or escape sequences.

In general my recommendation is to make a robust implementation in any system, which in the future will save you blushes and headaches. Fortunately, you do not have to write the code that takes care of the details.

Can you base yourself, for example, on the class TextFieldParser

A small example of use could be:

Using MyReader As New Microsoft.VisualBasic.FileIO.

    MyReader.TextFieldType = 
    MyReader.Delimiters = New String() {","}
    MyReader.CommentTokens = New String() {""}
    MyReader.HasFieldsEnclosedInQuotes = True
    Dim currentRow As String()
    'Loop through all of the fields in the file. 
    'If any lines are corrupt, report an error and continue parsing. 
    While Not MyReader.EndOfData
            currentRow = MyReader.ReadFields()

            Dim newRow = dTable.Rows.Add()
            newRow.ItemArray = currentRow

        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & 
            " is invalid.  Skipping")
        End Try
    End While
End Using
answered by 20.08.2016 / 03:20

You can find an example in the documentation , and it is important to know that by convention a file .CSV (Comma separated value) must have this structure to be considered as such:

a, Elenasys , 12345
b, Ronnie, 12345
c, Doru, 534543
d, Hassan, 6234234
e, Haris K., 78345345

This would be an example of how to convert the values obtained from a .csv to DataTable

    Dim lines = IO.File.ReadAllLines("C:\path\archivo.csv")
    Dim dTable = New DataTable
    Dim numeroColumnas = lines.First.Split(","c).Length
    For i As Int32 = 1 To numeroColumnas
        'Agrega columnas a DataTable.
        dTable.Columns.Add(New DataColumn("Columna_" & i, GetType(String)))
        Debug.Print("Columna_" & i)

    For Each line In lines
        Dim objFields = From field In line.Split(","c)
        Dim newRow = dTable.Rows.Add()
        newRow.ItemArray = objFields.ToArray()
        Debug.Print("columna_1: " & objFields.ToArray().GetValue(0).ToString)
        Debug.Print("columna_2: " & objFields.ToArray().GetValue(1).ToString)
        Debug.Print("columna_3: " & objFields.ToArray().GetValue(2).ToString)

answered by 20.08.2016 в 02:04