从 txtfile vb.net 获取数据
我正在寻找一种从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 LINQ 创建一个以第一列作为键的字典:
如果您不熟悉 LINQ(并且您正在使用 .NET 4.0),我建议使用
List(Of Tuple(Of String, List(Of String)))
因为重复项是允许(字典是没有选项)。http://msdn.microsoft.com/en-us/library/system .tuple.aspx
编辑:
如果您因为没有使用 .NET 4.0 而无法使用元组,请尝试按照“oldschool”方法创建
Dictionary(Of String, List (的列表(字符串)))
。它产生确切的期望结果(与其他方式不同,因为到目前为止我有点误解了您的要求):
如何读取值:
You could use LINQ to create a Dictionary with the first column as key:
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).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):
How you can read the values:
您可以使用StreamReader。并做这样的事情(抱歉它在 C# 中,但你明白了):
编辑:然后对于你的结构,你有一个字典:
IDictionary - 键:id 值:Truple 或创建你自己的元组类,如果你无权访问id 正如@Tim 建议的那样。
如果你可以做一些事情而不是使用通用的,我会建议这样做。
You could use the StreamReader. And do something like this (sorry its in c# but you get the idea):
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.