繁体   English   中英

如何读取特定的 csv 行 vb.net

[英]how to read a specific csv line vb.net

请求许可,我创建了一个机器人,使用 vb.net 和 selenium 将数据输入到 web。 从 csv 检索数据。 如何根据需要从csv中取数据,比如有100行,只取30-50行为例。 循环代码根本不应该循环。

 Dim textFieldParser As TextFieldParser = New TextFieldParser(TextBox1.Text) With
                {
                    .TextFieldType = FieldType.Delimited,
                    .Delimiters = New String() {","}
                }

        drv = New ChromeDriver(options)
        While Not textFieldParser.EndOfData
            Try
                Dim strArrays As String() = textFieldParser.ReadFields()
                Dim name As String = strArrays(0)
                Dim alamat As String = strArrays(1)
                Dim notlp As String = strArrays(2)
                drv.Navigate().GoToUrl("URL")
                Dim Nm = drv.FindElement(By.XPath("/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input"))
                Nm.SendKeys(name)
                Threading.Thread.Sleep(3000)
            Catch ex As Exception
            MsgBox("Line " & ex.Message & "is not valid and will be skipped.")

                End Try
        End While
    

谢谢

这是一个使用TextFieldParser读取特定行和特定行范围的示例。 请注意,我对行使用从零开始的索引。 如果要使用从 1 开始的行号,可以根据需要进行调整。

Public Function GetLine(filePath As String, index As Integer) As String()
    Using parser As New TextFieldParser(filePath) With {.Delimiters = {","}}
        Dim linesDiscarded = 0

        Do Until linesDiscarded = index
            parser.ReadLine()
            linesDiscarded += 1
        Loop

        Return parser.ReadFields()
    End Using
End Function

Public Function GetLines(filePath As String, startIndex As Integer, count As Integer) As List(Of String())
    Using parser As New TextFieldParser(filePath) With {.Delimiters = {","}}
        Dim linesDiscarded = 0

        Do Until linesDiscarded = startIndex
            parser.ReadLine()
            linesDiscarded += 1
        Loop

        Dim lines As New List(Of String())

        Do Until lines.Count = count
            lines.Add(parser.ReadFields())
        Loop

        Return lines
    End Using
End Function

简单的循环跳过和取线。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM