line.split之后的vb.net访问字段

时间:2016-03-24 06:17:54

标签: vb.net linq

我想使用if语句来更改/检查x(5) 如果x(5)= 270则x(5)= -90
有没有办法访问该阵列。

Sub Main()
    Dim filePath, filePathOut As String
    Dim fileName As String
    fileName = "test.txt"
    filePath =    System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, fileName)
    filePathOut = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "out\" & fileName)
    ' Create the IEnumerable data source.
    Dim lines As String() = System.IO.File.ReadAllLines(filePath)


    Dim lineQuery = From line In lines
                    Let x = line.Split(New Char() {","})
                    Order By x(0)
                    Select  x(0) & ", " & x(3) & ", " & x(5)

    System.IO.File.WriteAllLines(filePathOut, lineQuery.ToArray())

    Console.WriteLine("Spreadsheet2.csv written to disk. Press any key to exit")
    Console.ReadKey()
End Sub

3 个答案:

答案 0 :(得分:1)

使用linq的当前方法需要考虑的一件事是,是否存在与文本文件中的预期数据不符的行。由此可以解决以下问题: line没有预期的列数或行是空白的。

代码多于一行lambda或linq语句,但如上所示,允许使用断言。

Imports Microsoft.VisualBasic.FileIO
Public Class Form2

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim readFile As String =
            IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Textfile1.txt")

        Dim writeFile As String =
            IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Textfile2.txt")

        Dim Lines As New List(Of Line)

        Using reader As New TextFieldParser(readFile)

            reader.TextFieldType = FieldType.Delimited
            reader.Delimiters = New String() {","c}

            Dim currentRow As String()

            While Not reader.EndOfData
                '
                ' Feel free to discard the try/catch if so be it
                '
                Try
                    currentRow = reader.ReadFields()
                    Lines.Add(New Line(currentRow))

                Catch ex As MalformedLineException
                    ' decide how to handle
                End Try
            End While
        End Using

        IO.File.WriteAllLines(writeFile,
            Lines.OrderBy(Function(item) item.Column0) _
            .Select(Function(item) item.ItemArray).ToArray)

    End Sub
End Class
''' <summary>
''' Used to create a line item that will
''' be inserted into a List(Of Line) in the caller
''' reading a text file.
''' </summary>
''' <remarks>
''' Gave generic names to properties, we could do an
''' an anonymous type inside of Lambda or Linq and done
''' away with the class but felt this is simply another
''' option
''' </remarks>
Public Class Line
    Public Property Column0 As String
    Public Property Column1 As String
    Public Property Column2 As String
    Public Property Column3 As String
    Public Property Column4 As String
    Public Property Column5 As String
    Public Sub New(ByVal sender As String())
        If sender.Length = 6 Then
            Column0 = sender(0)
            Column1 = sender(1)
            Column2 = sender(2)
            Column3 = sender(3)
            Column4 = sender(4)
            If sender(5) = "270" Then
                Column5 = "-90"
            Else
                Column5 = "270"
            End If
        Else
            ' decide how to handle incorrect columns in a line
        End If
    End Sub
    Public Function ItemArray() As String
        Return String.Format("{0},{1},{2},{3},{4},{5}",
            Column0, Column1, Column2, Column3, Column4, Column5)
    End Function
End Class

答案 1 :(得分:0)

也许这样的事情会起作用。

[supervisord]
environment=CELERY_BROKER_URL="%(ENV_CELERY_BROKER_URL)s",FLASK_CONFIG="%(ENV_FLASK_CONFIG)s"

[program:celeryd]
command=celery worker -A celery --loglevel=info -P gevent -c 1000

可以找到更详细的答案here

答案 2 :(得分:0)

您是否有意为第3个CSV列有条件地返回值,类似这样的内容?

Dim lineQuery = From line In lines
                Let x = line.Split(New Char() {","})
                Order By x(0)
                Select  x(0) & ", " & x(3) & ", " & If(x(5) = "270", "-90", x(5))

供参考:Is there a conditional ternary operator in VB.NET?