从 txtfile vb.net 获取数据

发布于 2024-12-24 22:04:09 字数 894 浏览 0 评论 0原文

我正在寻找一种从 txt 文件读取数据的方法。文本文件具有这种结构(固定长度字段):

0000       AAAAAA     BBBBBB   CCCCCCCC
0000       JJJJJJ     III      RRRRRR
1111       XXXX       YYYYYYYY ZZZZZZZZ
1111       WW         PPPPPPPP ZZZZZZZZ
1111       XXXX       YYYYYYYY ZZZZZZZZ
2222       XXXX       YYYYYYYY ZZZZZZZZ
...

我必须按第一个字段将它们分组,在某种字典列表列表或类似的列表中。对于这个特定的示例,解决方案是:

id(list): 0000,1111,2222.....
(content)List: 0000
    field1(list): AAAAAA,JJJJJJ
    field2(list): BBBBBB,III
    field3(list): CCCCCCCC,RRRRRR
(content)List: 1111
    field1(list): XXXX,WW,XXXX
    field2(list): YYYYYYYY,PPPPPPPP,YYYYYYYY 
    field3(list): ZZZZZZZZ,ZZZZZZZZ,ZZZZZZZZ
(content)List: 2222
    field1(list): XXXX...
    field2(list): YYYYYYYY...
    field3(list): ZZZZZZZZ...

现在我将整个 txt 存储在字符串列表中(每行一个)。

我怎样才能在 vbnet 中做到这一点?您认为有更好的方法来解决这个问题吗?

谢谢,祝你新年快乐

i'm looking for a method for reading data from a txt file. The text file have this kind of structure (fixed length fields):

0000       AAAAAA     BBBBBB   CCCCCCCC
0000       JJJJJJ     III      RRRRRR
1111       XXXX       YYYYYYYY ZZZZZZZZ
1111       WW         PPPPPPPP ZZZZZZZZ
1111       XXXX       YYYYYYYY ZZZZZZZZ
2222       XXXX       YYYYYYYY ZZZZZZZZ
...

I have to get them in groups by first field, in somekind of list of dictionary lists or something like this. For this particular example the solution would be:

id(list): 0000,1111,2222.....
(content)List: 0000
    field1(list): AAAAAA,JJJJJJ
    field2(list): BBBBBB,III
    field3(list): CCCCCCCC,RRRRRR
(content)List: 1111
    field1(list): XXXX,WW,XXXX
    field2(list): YYYYYYYY,PPPPPPPP,YYYYYYYY 
    field3(list): ZZZZZZZZ,ZZZZZZZZ,ZZZZZZZZ
(content)List: 2222
    field1(list): XXXX...
    field2(list): YYYYYYYY...
    field3(list): ZZZZZZZZ...

Right now i have the the whole txt stored in a list of strings (one per line).

How can i do this in vbnet? Do you think there's a better approach to this problem?

Thanks and have a happy new year

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

殊姿 2024-12-31 22:04:09

您可以使用 LINQ 创建一个以第一列作为键的字典:

Dim fileName = "C:\Temp\Test.txt"
Dim allLines = IO.File.ReadAllLines(fileName)

Dim query = From line In allLines
        Select Columns = Microsoft.VisualBasic.Split(line, vbTab)
        Select ID = If(Columns.Count <> 0, Columns(0), " "), Values = Columns.Skip(1).ToList
        Group By ID Into Group
        Select ID, Group

' create a Dictionary from the LINQ-Query '
Dim dict = query.ToDictionary(Function(grp) (grp.ID))
' iterate all Dictionary-Entries '
For Each entry In dict
    Dim key = entry.Key ' f.e 0000 
    For Each grp In entry.Value.Group
        Dim id = grp.ID ' f.e 0000 (can repeat how we see in your example) '
        Dim values As List(Of String) = grp.Values
        ' f.e. (0) "AAAAAA"  (1) "BBBBBB" (2) "CCCCCCCC" '
    Next
Next

如果您不熟悉 LINQ(并且您正在使用 .NET 4.0),我建议使用 List(Of Tuple(Of String, List(Of String))) 因为重复项是允许(字典是没有选项)。

Dim data = New List(Of Tuple(Of String, List(Of String)))
For Each line In allLines
    Dim Columns = Microsoft.VisualBasic.Split(line, vbTab)
    Dim ID = If(Columns.Count <> 0, Columns(0), "[empty-line]")
    data.Add(Tuple.Create(ID, Columns.ToList))
Next
' iterate the collection and read values '
For Each item In data
    Dim ID = item.Item1
    Dim Columns = item.Item2
Next

http://msdn.microsoft.com/en-us/library/system .tuple.aspx


编辑

如果您因为没有使用 .NET 4.0 而无法使用元组,请尝试按照“oldschool”方法创建 Dictionary(Of String, List (的列表(字符串)))

它产生确切的期望结果(与其他方式不同,因为到目前为止我有点误解了您的要求):

Dim allLines = IO.File.ReadAllLines(fileName)
Dim data As New Dictionary(Of String, List(Of List(Of String)))
For Each line In allLines
    Dim cols = line.Split(ControlChars.Tab)
    Dim ID As String
    If cols.Length <> 0 AndAlso cols(0).Length <> 0 Then
        ID = cols(0)
        If data.ContainsKey(ID) Then
            Dim columnLists = data(ID)
            For colIndex As Int32 = 1 To cols.Length - 1 'skip first(id)-column
                If columnLists.Count >= colIndex Then
                    Dim columnList = columnLists(colIndex - 1)
                    columnList.Add(cols(colIndex))
                Else
                    Dim newColumnList As New List(Of String)
                    newColumnList.Add(cols(colIndex))
                    columnLists.Add(newColumnList)
                End If
            Next
        Else
            Dim columnLists = New List(Of List(Of String))
            For colIndex As Int32 = 1 To cols.Length - 1 'skip first(id)-column
                Dim newColumnList As New List(Of String)
                newColumnList.Add(cols(colIndex))
                columnLists.Add(newColumnList)
            Next
            data.Add(ID, columnLists)
        End If
    End If
Next

如何读取值:

Dim idList = data.Keys ' List of all ID-Keys '
For Each id As String In idList
    Dim content As List(Of List(Of String)) = data(id)
    Dim field1List As List(Of String) = content(0)     ' AAAAAA,JJJJJJ
    Dim field2List As List(Of String) = content(1)     ' BBBBBB,III
    ' ....
Next

You could use LINQ to create a Dictionary with the first column as key:

Dim fileName = "C:\Temp\Test.txt"
Dim allLines = IO.File.ReadAllLines(fileName)

Dim query = From line In allLines
        Select Columns = Microsoft.VisualBasic.Split(line, vbTab)
        Select ID = If(Columns.Count <> 0, Columns(0), " "), Values = Columns.Skip(1).ToList
        Group By ID Into Group
        Select ID, Group

' create a Dictionary from the LINQ-Query '
Dim dict = query.ToDictionary(Function(grp) (grp.ID))
' iterate all Dictionary-Entries '
For Each entry In dict
    Dim key = entry.Key ' f.e 0000 
    For Each grp In entry.Value.Group
        Dim id = grp.ID ' f.e 0000 (can repeat how we see in your example) '
        Dim values As List(Of String) = grp.Values
        ' f.e. (0) "AAAAAA"  (1) "BBBBBB" (2) "CCCCCCCC" '
    Next
Next

If you are not familiar with LINQ (and you are using .NET 4.0) i would suggest to use a List(Of Tuple(Of String, List(Of String))) since duplicates are allowed(dictionary is no option).

Dim data = New List(Of Tuple(Of String, List(Of String)))
For Each line In allLines
    Dim Columns = Microsoft.VisualBasic.Split(line, vbTab)
    Dim ID = If(Columns.Count <> 0, Columns(0), "[empty-line]")
    data.Add(Tuple.Create(ID, Columns.ToList))
Next
' iterate the collection and read values '
For Each item In data
    Dim ID = item.Item1
    Dim Columns = item.Item2
Next

http://msdn.microsoft.com/en-us/library/system.tuple.aspx


Edit:

If you cannot use Tuples because you are not using .NET 4.0, try following "oldschool"-approach that creates a Dictionary(Of String, List(Of List(Of String))).

It produces the exact desired result (unlike the other ways, because i've misunderstood your requirement a little bit so far):

Dim allLines = IO.File.ReadAllLines(fileName)
Dim data As New Dictionary(Of String, List(Of List(Of String)))
For Each line In allLines
    Dim cols = line.Split(ControlChars.Tab)
    Dim ID As String
    If cols.Length <> 0 AndAlso cols(0).Length <> 0 Then
        ID = cols(0)
        If data.ContainsKey(ID) Then
            Dim columnLists = data(ID)
            For colIndex As Int32 = 1 To cols.Length - 1 'skip first(id)-column
                If columnLists.Count >= colIndex Then
                    Dim columnList = columnLists(colIndex - 1)
                    columnList.Add(cols(colIndex))
                Else
                    Dim newColumnList As New List(Of String)
                    newColumnList.Add(cols(colIndex))
                    columnLists.Add(newColumnList)
                End If
            Next
        Else
            Dim columnLists = New List(Of List(Of String))
            For colIndex As Int32 = 1 To cols.Length - 1 'skip first(id)-column
                Dim newColumnList As New List(Of String)
                newColumnList.Add(cols(colIndex))
                columnLists.Add(newColumnList)
            Next
            data.Add(ID, columnLists)
        End If
    End If
Next

How you can read the values:

Dim idList = data.Keys ' List of all ID-Keys '
For Each id As String In idList
    Dim content As List(Of List(Of String)) = data(id)
    Dim field1List As List(Of String) = content(0)     ' AAAAAA,JJJJJJ
    Dim field2List As List(Of String) = content(1)     ' BBBBBB,III
    ' ....
Next
浅浅淡淡 2024-12-31 22:04:09

您可以使用StreamReader。并做这样的事情(抱歉它在 C# 中,但你明白了):

using(StreamReader r = new StreamReader("@C:\somefile.txt"))
{
    while(!r.EndOfStream)
    {
        string line = r.ReadLine();
        string[] data = line.Split(new string[] { "    " }); //use a seperator that is approriate
        //constructor the dictionary yourself
    }
}

编辑:然后对于你的结构,你有一个字典:

IDictionary - 键:id 值:Truple 或创建你自己的元组类,如果你无权访问id 正如@Tim 建议的那样。

如果你可以做一些事情而不是使用通用的,我会建议这样做。

public class Tuple<T,Q,R>
{
    public T First { get; set; }
    public Q Second { get; set; }
    public R Third { get; set; }
}

You could use the StreamReader. And do something like this (sorry its in c# but you get the idea):

using(StreamReader r = new StreamReader("@C:\somefile.txt"))
{
    while(!r.EndOfStream)
    {
        string line = r.ReadLine();
        string[] data = line.Split(new string[] { "    " }); //use a seperator that is approriate
        //constructor the dictionary yourself
    }
}

Edit: Then for your Structure you have a dictionary:

IDictionary - Key: id Value: Truple or create your own tuple class if you don't have access to id as @Tim suggested.

if you can do something rather then using generic I would suggest that.

public class Tuple<T,Q,R>
{
    public T First { get; set; }
    public Q Second { get; set; }
    public R Third { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文