1

我有一个制表符分隔的数据文件。每个对象用 2 个换行符相互分隔,每个对象的第一行和第三行是列名。

我的标签分隔文件

ID         [TAB] NAME     
001        [TAB] Croline            
DATE       [TAB] DOC
30/06/2010 [TAB] 101435

2 x EMPTY LINE                      

ID         [TAB] NAME     
002        [TAB] Grek            
DATE       [TAB] DOC   
30/06/2010 [TAB] 101437

2 x EMPTY LINE

...........
...........

我的对象等级

Public Class MyObject
    Public Property Id As String
    Public Property Name As String
    Public Property Date As String
    Public Property Doc As String
End Class

如何将此文件读入 MyObjects?

4

3 回答 3

1

解决方案将类似于(伪代码):

Create an empty list of MyObjects
Open file for reading
While there are lines left to read:
    create a MyObject instance i
    read a line and ignore it.
    read a line into s1
    split s1 at tab character into a and b
    set i.Id to a1
    set i.Name to b1
    read a line and ignore it
    read a line into s2
    split s2 at tab character into a and b
    set i.Date to a2
    set i.Doc to b2
    add i to your list

    read a line and ignore it.
    read a line and ignore it.

将其翻译成 vb.net 留给读者作为练习。

于 2013-08-19T13:01:43.303 回答
1

如果不知道,更具体地说,您在该任务的哪一部分遇到问题,很难帮助您理解如何执行此操作,但也许是一个简单的工作示例可以帮助您入门。

如果您像这样定义您的数据类:

Public Class MyObject
    Public Property Id As String
    Public Property Name As String
    Public Property [Date] As String  ' Note that "Date" must be surrounded with brackets since it is a keyword in VB
    Public Property Doc As String
End Class

然后你可以像这样加载它:

' Create a list to hold the loaded objects
Dim objects As New List(Of MyObject)()

' Read all of the lines from the file into an array of strings
Dim lines() As String = File.ReadAllLines("test.txt")

' Loop through the array of lines from the file.  Step by 7 each 
' time so that the current value of "i", at each iteration, will 
' be the index of the first line of each object
For i As Integer = 0 To lines.Length Step 7
    If lines.Length >= i + 3 Then

        ' Create a new object to store the data for the current item in the file
        Dim o As New MyObject()

        ' Get the values from the second line
        Dim fields() As String = lines(i + 1).Split(ControlChars.Tab)
        o.Id = fields(0)
        o.Name = fields(1)

        ' Get the values from the fourth line
        fields = lines(i + 3).Split(ControlChars.Tab)
        o.Date = fields(0)
        o.Doc = fields(1)

        ' Add this item to the list
        objects.Add(o)
    End If
Next

加载它的代码非常基本。它不会进行额外的验证来确保文件中的数据格式正确,但是,如果文件有效,它将可以将数据加载到对象列表中。

于 2013-08-19T13:03:36.973 回答
0

我不是为该数据编写特定代码,而是首先将其转换为简单的 CSV。如果这只是一次性的事情,这可能是有道理的。

1)在记事本++中加载文件

2)替换\t;(使用扩展搜索模式),为您提供这种数据:

ID;NAME
001;Croline
DATE;DOC
30/06/2010;101435


ID;NAME
002;Grek
DATE;DOC
30/06/2010;101437

DATE;DOC3)通过搜索DATE;DOC\n并替换为删除所有行;(您可能需要执行Edit > EOL Conversions > UNIX Format才能工作)

ID;NAME4)用保证不会在数据中使用的占位符符号替换所有行,也许是£. 您的数据应如下所示:

£001;Croline
;30/06/2010;101435


£002;Grek
;30/06/2010;101437

5) 执行Edit > Blank Operations > Remove Unnecessary Blank 和 EOL,这应该将所有数据放在一行上。

6)搜索并替换您的占位符£符号\n

7)删除顶部多余的空白行。瞧,你有一个 CSV 文件。

于 2013-08-19T13:25:50.817 回答