简体   繁体   English

在VB.NET中使用带有数组的CSV .txt文件来编辑数据?

[英]Using CSV .txt files with arrays in VB.NET to edit data?

I'm a beginner trying to learn VB.NET and I'm not quite sure how I'll explain this, but I'll give it a shot. 我是初学者,正在尝试学习VB.NET,我不太清楚我将如何解释这一点,但我会试一试。 Basically, I've written a txt file with about 10 lines of data in CSV form. 基本上,我已经用CSV格式编写了一个包含大约10行数据的txt文件。

For example: 例如:

John, 10, 14
Michael, 14, 27
Billy, 13, 45 
etc, etc....

I just want to be able to read and edit particular lines - not necessarily add new lines. 我只是想能够阅读和编辑特定的行 - 不一定要添加新的行。

Just wondering if someone could just outline how I'd go about this - not asking anyone to write the program for me. 只是想知道是否有人可以概述我将如何做到这一点 - 不要求任何人为我编写程序。 I just don't know what to do and I couldn't understand other answers I've found on SO that attempted to solve the same problem. 我只是不知道该怎么做,我无法理解我在SO上发现的其他答案,试图解决同样的问题。 I don't know if I'm just a bit dense or something so it'd be great if someone could perhaps give a simple, 'dumbed-down' outline of what I need to do. 我不知道我是否只是有点密集或者其他什么东西所以如果有人能够给出我需要做的简单,“愚蠢”的轮廓,那就太棒了。

Thank you. 谢谢。

as stated in the comments you usually read all the file into memory, manipulate it and write it all back. 如评论中所述,您通常会将所有文件读入内存,对其进行操作并将其全部写回。

dotnet has a method that puts a file content in an array (line by line) dotnet有一个方法将文件内容放在一个数组中(逐行)

if you need to access individual cells in the CSV you probably want to run the split method on each line and join to merge them back together. 如果您需要访问CSV中的单个单元格,您可能希望在每一行上运行split方法并加入以将它们合并回来。

the split/join methods are either an method of the string/array object or its in the Strings namespace split / join方法可以是字符串/数组对象的方法,也可以是字符串命名空间中的方法

a method for writing the file back is also in the System.IO Namespace 用于写回文件的方法也在System.IO命名空间中

You don't asking to write the code for you, I can understand you but I think there is no way to show how can you do that. 你没有要求为你编写代码,我可以理解你,但我认为没有办法证明你怎么能这样做。 I used split/join methods as @weberik mentioned. 我使用了@weberik提到的分割/连接方法。 It is a simple console application: 这是一个简单的控制台应用程序

Public Class Sample

    Public Shared Sub Main()
        CreateExampleFileIfNotExists()
        'lines variable is an "Array"
        Dim lines() As String = IO.File.ReadAllLines("Example.txt")
        'write items to the console
        ShowItems(lines, "Values before edit:")
        'edit the items
        EditItems(lines)
        'write edited items to the console
        ShowItems(lines, "Values after edit:")
        'save changes?
        Save(lines)
        'finish
        Console.WriteLine("Press any key to exit.")
        Console.ReadKey()
    End Sub

    Public Shared Sub ShowItems(lines() As String, header As String)
        Console.WriteLine(header)
        Dim headers() As String = {"Name:", "Value1:", "Value2:"}
        WriteItems(headers)
        For Each line In lines
            WriteItems(line.Split(","))
        Next
    End Sub

    Public Shared Sub EditItems(lines() As String)
        For i As Integer = 0 To lines.Length - 1
            Dim line As String = lines(i)
            Dim values() As String = line.Split(",")
            'edit the item
            values(0) = "Edited " & values(0)
            values(1) += i
            values(2) *= i
            lines(i) = String.Join(",", values)
        Next
        Console.WriteLine() 'empty line
    End Sub

    Public Shared Sub WriteItems(itemValues() As String)
        Dim line As String = ""
        For Each item In itemValues
            line &= item & vbTab & vbTab
        Next
        Console.WriteLine(line)
    End Sub

    Public Shared Sub CreateExampleFileIfNotExists()
        If Not IO.File.Exists("Example.txt") Then
            IO.File.WriteAllLines("Example.txt", {"John,10,14", "Michael,14,27", "Billy,13,45"})
        End If
    End Sub

    Public Shared Sub Save(lines() As String)
        Console.WriteLine(vbCrLf & "Do you want to save the changes? Y/N")
        Dim result = Console.ReadKey()
        Console.WriteLine()
        Select Case result.Key
            Case ConsoleKey.Y
                IO.File.WriteAllLines("Example.txt", lines)
                Console.WriteLine("The changes has been saved.")
        End Select
    End Sub

End Class

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

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