我正在尝试在文本文件中将日期时间之间的文本分段。我对编程知识非常有限,所以任何帮助都会受到赞赏。
这是我目前的阅读代码,但我遇到了很多问题。建议?
Private Sub Read_report_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim dict_report As New Dictionary(Of DateTime, String)
Dim strDateTimeKey As String = DateTime.Now.ToString()
Dim strRawValue As String = "sdfsdfsdf\n\nsdfsdfsdf"
Dim dateTimeKey As String = DateTime.Parse(Text)
dict_report.Add(dateTimeKey, strRawValue)
Console.WriteLine(dict_report(dateTimeKey).ToString())
End Sub
Function Read() As Dictionary(Of DateTime, String)
Dim dict_report As New Dictionary(Of DateTime, String)
Dim sR As StreamReader = New StreamReader("systemrequest.txt")
Dim strInfo As String = ""
Dim prevDateTimeKey As New DateTime
Dim currentline As String = sR.ReadLine()
prevDateTimeKey = DateTime.Parse(currentline)
Do While Not currentline Is Nothing
Dim dateTimeKey As New DateTime
If DateTime.TryParse(currentline, dateTimeKey) Then
dict_report.Add(prevDateTimeKey, strInfo)
prevDateTimeKey = dateTimeKey
strInfo = ""
Else
strInfo += "\n" + currentline
End If
currentline = sR.ReadLine()
Loop
'dict_report.Item(DateTime.Now)
sR.Close()
Return dict_report
目前它返回的错误是与prevDateTimekey具有相同的密钥。
正在读取的原始数据如下:
27/05/2015 12:20:32 PM
wat0021
HARDWARE ISSUE1
27/05/2015 1:22:49 PM
Username
HARDWARE ISSUE2
27/05/2015 1:53:16 PM
Username
HARDWARE ISSUE3
27/05/2015 10:37:44 PM
Username
HARDWARE ISSUE3
27/05/2015 10:39:42 PM
Username
HARDWARE ISSUE4
27/05/2015 10:39:58 PM
Username
HARDWARE ISSUE5
27/05/2015 10:41:03 PM
Username
HARDWARE ISSUE
27/05/2015 10:42:08 PM
Username
HARDWARE ISSUE
27/05/2015 10:43:25 PM
Username
HARDWARE ISSUE
27/05/2015 10:43:58 PM
Username
HARDWARE ISSUE
27/05/2015 10:44:45 PM
Username
HARDWARE ISSUE
答案 0 :(得分:0)
以下是我如何处理这个问题。首先,存储条目信息的类:
Public Class HardwareIssue
Public Property UserName As String
Public Property ItemDate As DateTime
Public Property Report As List(Of String)
Public Sub New()
Report = New List(Of String)
End Sub
End Class
您可以将它们连接成一个字符串,而不是报表数据的字符串集合。我不知道哪个更合适。
然后处理文件,为每个“块”创建一个新的硬件问题项。然后将其中的大部分存储在List(of HardwareIssue)
中。注意:最终代码可能需要调整,具体取决于我们不知道的实际数据中的任何“怪癖”。一个例子是畸形或不完整的块 - 补救措施取决于原因。
Dim item As HardwareIssue
Dim dtItem As DateTime
Dim txt As String
Dim HWItems As New List(Of HardwareIssue)
Using sr As New StreamReader("C:\Temp\hwreport.txt")
' get a line
txt = sr.ReadLine
' loop until it is empty
Do Until String.IsNullOrEmpty(txt)
' new item
item = New HardwareIssue
' the txt is a Date, it is the start of a "block"
If DateTime.TryParse(txt, dtItem) Then
item.ItemDate = dtItem ' store date
dtItem = Nothing ' clear temp var
item.UserName = sr.ReadLine ' read and store username
txt = sr.ReadLine ' read a line
' loop to keep reading report text until
' we run out of lines OR see a date
Do Until ((String.IsNullOrEmpty(txt)) Or (DateTime.TryParse(txt, dtItem)))
item.Report.Add(txt) ' add report text
txt = sr.ReadLine ' read a line
Loop
HWItems.Add(item) ' add this item to the list
Else
' this is bad news: the first line needs to be a date
' read a new line to find a date, but this might skip a
' partial first block
txt = sr.ReadLine
End If
' if txt from the last sr.Readline in the Report DO loop is empty
' the job is done, exit. If it is a Date which ended
' the loop, txt still contains it and its ready for the
' TryParse test at the top of the loop
Loop
End Using ' close, dispose of sr
Return HWItems