Results 1 to 5 of 5

Thread: Reading only one field from CSV file using TextFieldParser.ReadFields Method

  1. #1

    Thread Starter
    New Member james1628's Avatar
    Join Date
    Sep 2020
    Posts
    5

    Reading only one field from CSV file using TextFieldParser.ReadFields Method

    I am trying to read a particular field of a CSV file using ReadFields Method of TextFieldParser class. I will use the example at Microsoft to to show what I want to do.

    https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.fileio.textfieldparser.readfields?view=netcore-3.1#Microsoft_VisualBasic_FileIO_TextFieldParser_ReadFields

    Code:
    Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\ParserText.txt")
        MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
        MyReader.Delimiters = New String() {","}
        Dim currentRow As String()
        
        While Not MyReader.EndOfData
            Try
                currentRow = MyReader.ReadFields()
        
                'How to read only one field instead of a row in this section?
    
                For Each currentField As String In currentRow
                    My.Computer.FileSystem.WriteAllText(
                        "C://testfile.txt", currentField, True)
                Next
             Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
                MsgBox("Line " & ex.Message & " is invalid.  Skipping")
            End Try
        End While
    End Using
    
    From Microsoft's example, how do I get to retrieve, for example, only the second field of a 3-fields CSV file?
    Last edited by james1628; Sep 16th, 2020 at 08:01 PM. Reason: improve title

  2. #2
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,046

    Re: Reading only one field from CSV file using TextFieldParser.ReadFields Method

    this should help
    Code:
        Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
            Dim Dateiname As String = "D:\TestFolder\Users.txt"
            Using csvParser As New TextFieldParser(Dateiname)
                With csvParser
                    .SetDelimiters(";")
                    .HasFieldsEnclosedInQuotes = False ' or True 
    
                    'my File has a Header
                    Dim Header As String = .ReadLine()
                    Dim FieldData() As String
                    Do While Not .EndOfData
                        FieldData = .ReadFields()
                        ' Debug.Print(FieldData(0))  ' 1st Field
                        Debug.Print(FieldData(1))  ' 2nd Field
                        'Debug.Print(FieldData(2))  ' 3rd Field
                    Loop
                End With
            End Using
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,300

    Re: Reading only one field from CSV file using TextFieldParser.ReadFields Method

    ReadFields returns a String array. How would you usually get a specific element from an array?

    One of the main reasons that beginners have trouble solving problems is that they don't know what problem they're trying to solve. Your problem really has nothing to do with TextFieldParsers. You already know how to read data using a TextFieldParser so that's done. What you have is an array and all arrays are the same, regardless of where they come from. Getting a specific element from this array is the same as getting a specific element from any other array, so that's the problem you actually need to solve. If you already know how to work with arrays then you already know the answer to your question. If you don't, you should go an learn about arrays and then you will know the answer to your question. I would suggest that anyone who is trying to read data from a file using a TextFieldParser should have learned about arrays already, given that they are one of the fundamentals of programming.

  4. #4

    Thread Starter
    New Member james1628's Avatar
    Join Date
    Sep 2020
    Posts
    5

    Re: Reading only one field from CSV file using TextFieldParser.ReadFields Method

    Quote Originally Posted by ChrisE View Post
    this should help
    Thank you, ChrisE.

  5. #5

    Thread Starter
    New Member james1628's Avatar
    Join Date
    Sep 2020
    Posts
    5

    Re: Reading only one field from CSV file using TextFieldParser.ReadFields Method

    Quote Originally Posted by jmcilhinney View Post
    ReadFields returns a String array. How would you usually get a specific element from an array?
    Thank you.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width