字符串构建器(解析垂直显示的记录) [英] String builder (Parsing vertically presented records)

查看:30
本文介绍了字符串构建器(解析垂直显示的记录)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习Visual Basic(VB.NET 03),我需要做这个小程序,这将读取我们从另一个获得的文本文件

拥有调查数据的公司,解析它并将其展平并从其中的记录中单个字符串生成


这个空间分隔文件与这些组中的许多示例之间的主要区别是数据不是水平呈现,而是垂直呈现。你可以看看下面的一个例子。


从这个Visual Basic程序首先要做的就是忽略

文本文件的第一行(标题)。然后,从第一个调查记录中提取RegID

,用他们的答案获取所有25个数字

(其中一些是空白的,其中一些是数字)并转换它

成一个字符串。完成后,我需要回车,所以

下一个调查记录可能会变平。有一件事可能会让我更容易获得一条说NewRegID的行。当一个新的

调查记录开始时。


我需要最终数据如下:


214555134 ,1,Y,2,N,3,Y,4,1,5,Y,6,Y,7,Y,8,Y,9,1,10,Y,11,...一路

到25,2。

214016421,1,Y,2,Y,3,Y,..等等。


,它将保存为文本文件。


到目前为止,这是我在命令按钮中的内容。这基本上打开了带有StreamReader的文本文件,并逐行读取它并将它放在一个名为txtStrings的文本框中。

。我错过了最重要的部分:

记录构建器和字符串创建。

Dim StrFileName As String

strFileName =" C :\Survey01.txt"

如果不是System.IO.File.Exists(StrFileName)那么

MsgBox(文件不存在。)

退出Sub

结束如果


Dim strRdr As System.IO.StreamReader =

System.IO .File.OpenText(StrFileName)


Dim StrLine As String

StrLine = strRdr.ReadLine()

直到StrLine是什么都没有
txtStrings.AppendText(StrLine& vbCrLf)

StrLine = strRdr.ReadLine()

循环

strRdr.Close()


Survey1.txt示例:


RegID ItemName响应

214555134 NewRegID

214555134 1 Y

214555134 2 N

214555134 3 Y

214555134 4 1

214555134 5 Y

214555134 6 Y

214555134 7 Y

214555134 8 Y

214555134 9 1

214555134 10 Y

214555134 11 Y

214555134 12 Y

214555134 13 Y

214555134 14 Y

214555134 15 1

214555134 16

214555134 17 Y

214555134 18 Y

214555134 19 Y

214555134 20 Y

214555134 21 1

214555134 22 N

214555134 23 N

214555134 24 1

214555134 25 2

214016421 NewRegID

214016421 1 Y

214016421 2 Y

214016421 3 Y

214016421 4 1

214016421 5 Y

214016421 6

214016421 7 Y

214016421 8 Y

214016421 9 1

214016421 10 Y

214016421 11 N

214016421 12 Y

214016421 13 Y

214016421 14 Ï$>
214016421 15 1

214016421 16 Y

214016421 17

214016421 18 Y

214016421 19 Y

214016421 20 Y

21401642 1 21 1

214016421 21 1

214016421 22 Y

214016421 23 N

214016421 24 2
214016421 25 3

213565432 1 Y

213565432 2 N

213565432 3 N

。 ..

EOF


任何帮助都会非常感激。


谢谢!

解决方案

IL***@NETZERO.NET 写道:

我刚开始学习Visual Basic(VB.NET 03),我需要做这个小程序,它将读取我们从另一家公司获得的文本文件有调查数据,解析它并将其展平,并从其中的记录中取出单个字符串。
这个空间分隔文件与这些组中的许多示例之间的主要区别是,数据不是水平呈现,而是垂直呈现。你可以在下面看一个例子。

我需要从这个Visual Basic程序首先要做的就是忽略文本文件的第一行(标题)。然后,从第一个调查记录中提取RegID
,获取所有25个数字及其答案
(其中一些是空白的,其中一些是数字)并将其转换为单个字符串。完成后,我需要回车,以便下一个调查记录可以变平。可能会使这更容易的一件事是,我确实得到一条说NewRegID的行。当新的
调查记录开始时。

我需要最终数据如下:

214555134,1,Y,2,N,3,Y ,4,1,5,Y,6,Y,7,Y,8,Y,9,1,10,Y,11,......一直到25,2。
214016421,1,Y,2,Y,3,Y,..等等。

它将被保存为文本文件。

到目前为止,这个是我在命令按钮中的内容。这基本上用StreamReader打开文本文件,逐行读取并将其放在一个名为txtStrings的文本框中。我错过了最重要的部分:
记录构建器和字符串创建。

Dim StrFileName As String
strFileName =" C:\Survey01.txt" <如果不是System.IO.File.Exists(StrFileName)那么
MsgBox(文件不存在。)
退出Sub
结束如果

Dim strRdr As System.IO.StreamReader =
System.IO.File.OpenText(StrFileName)

Dim StrLine As String
StrLine = strRdr.ReadLine() txtStrings.AppendText(StrLine& vbCrLf)
StrLine = strRdr.ReadLine()
循环
strRdr.Close()
<调查1.txt示例:

RegID ItemName响应
214555134 NewRegID
214555134 1 Y
214555134 2 N
214555134 3 Y
214555134 4 1
214555134 5 Y
214555134 7 Y
214555134 8 Y
214555134 9 1
214555134 10 Y
214555134 11 Y
214555134 12 Y
21455 5134 13 Y
214555134 14 Y
214555134 15 1
214555134 16
214555134 17 Y
214555134 18 Y
214555134 19 Y
214555134 20是
214555134 21 1
214555134 22 N
214555134 24 1
214555134 25 2
214016421 NewRegID
214016421 1 Y
214016421 2 Y
214016421 4 1
214016421 5 Y
214016421 6
214016421 7 Y
214016421 8 Y 214016421 9 1
214016421 10 Y
214016421 12 Y
214016421 13 Y
214016421 14 Y
214016421 15 1
214016421 16 Y
214016421 17
214016421 18 Y
214016421 20 Y
214016421 21 1
214016421 21 1
214016421 22 Y
214016421 23 N
214016421 24 2
214016421 25 3
213565432 1 Y
213565432 2 N
213565432 3 N
.. < EOF

任何帮助都将不胜感激。

谢谢!




也许这会对你有所帮助。你可能想使用String.Split

函数来获取个人物品。


Dim StrFileName As String

strFileName = C:\Survey01.txt

如果不是System.IO.File.Exists(StrFileName)那么

MsgBox(文件不存在。 )

退出Sub

结束如果


Dim strRdr As System.IO.StreamReader =

System.IO.File.OpenText(StrFileName)

Dim StrLine As String

Dim StringArray()as String

执行strRdr.Peek< > -1

StrLine = strRdr.ReadLine()

如果StrLine.EndsWith(NewRegID)那么

''这是一个新记录

StringArray = StrLine.Split("")

''StringArray(0)是你的新ID

else

''这个持续记录

StringArray = StrLine.Split("")

''String(0)有Column1

''String(1)有Column2

''String(2)有Column3(如果有的话)

for ii as integer = 1 to String.Getupperbound(0 )

''在这里附加你的字符串

next

结束如果

循环

strRdr.Close()


你好EOF


你的意思是什么(这里打字没有检查所以看错字或其他什么)


Dim StrLine As String
StrLine = strRdr.ReadLine()
dim sb as new text.stringbuilder(StrLine.Substring(0, 9))直到StrLine没有
dim fields()= StrL ine.Split()

sb.append(fields(1)& fields(2))StrLine = strRdr.ReadLine()
如果StrLine不是什么并且也是

strline.indexof(" NewRegID")> -1然后

strWrt.WriteLine(sb.ToString)

sb = new text.Stringbuilder(StrLine.Substring(0,9))

结束如果循环
strRdr.Close()




我希望这有帮助,


Cor





另一个实现:


========= ==========

Private Sub ReadText()

Dim fsr As New FileStream(" VertFile.txt",FileMode.Open,

FileAccess.Read)

Dim fsw As New FileStream(" ResultFile.txt",FileMode.Create,

FileAccess.Write)

Dim sr作为新StreamReader(fsr)

Dim sw作为新StreamWriter(fsw)


将此线条变暗作为字符串

Dim lineContents()As String


''忽略文本文件的第一行

sr.ReadLine()

而sr.Peek> -1

thisLine = sr.ReadLine()

lineContents = thisLine.Split(New Char(){"" c})

如果lineContents(1).Trim =" NewRegID"那么

''我们有了新纪录。写下sb的当前值并插入新的

行。

如果sb.Length> 0然后

''用句号替换最后一个逗号。

sb.Replace(",","。",sb.Length - 1, 1)

sw.WriteLine(sb.ToString())

结束如果

sb = New StringBuilder()

sb.Append(lineContents(0))

sb.Append(",")

Else

''这是同样的记录。继续添加文字

sb.Append(lineContents(1))

sb.Append(",")

if sb.Length > 2然后

sb.Append(lineContents(2))

sb.Append(",")

End If

结束如果

结束时

''用句号替换最后一个逗号。

sb.Replace(", ","。",sb.Length - 1,1)

sw.WriteLine(sb.ToString())

sw.Flush()

''清理

sw.Close()

sr.Close()

fsr.Close()

fsw.Close()

结束子

===================


请注意,您的第三组记录(213565432)没有行

" NewRegID"。

当该行时插入样本文本,然后就可以了。


问候,


Cerebrus。


Hi, I just started learning Visual Basic (VB.NET 03) and I need to do
this small program that will read a text file we get from another
company that has survey data, parse it and flatten it out and make
single strings out of the records in it. The major difference between
this space delimited file from the many examples in these groups is
that the data is not presented horizontally, but vertically. You can
see an example below.

What I need from this Visual Basic procedure to do first is to ignore
the first line of the text file (headings). Then, extract the RegID
from the first survey record, get all 25 numbers with their answers
(some of them are blank and some of them are numbers) and convert it
into a single string. After this is done, I need a carriage return so
the next survey record can be flatten out. One thing that might make
this easier is that I do get a line that says "NewRegID" when a new
survey record starts.

I need the final data to look like this:

214555134,1,Y,2,N,3,Y,4,1,5,Y,6,Y,7,Y,8,Y,9,1,10,Y ,11, ... all the way
to 25,2.
214016421,1,Y,2,Y,3,Y, .. and so on.

and it will be saved as a text file.

So far, this is what I have in my command button. This basically opens
the text file with StreamReader, reads it line by line and places it
in a text box called txtStrings. I''m missing the most important part:
the record builder and string creation.
Dim StrFileName As String
strFileName ="C:\Survey01.txt"
If Not System.IO.File.Exists(StrFileName) Then
MsgBox("File does not exists.")
Exit Sub
End If

Dim strRdr As System.IO.StreamReader =
System.IO.File.OpenText(StrFileName)

Dim StrLine As String
StrLine = strRdr.ReadLine()
Do Until StrLine Is Nothing
txtStrings.AppendText(StrLine & vbCrLf)
StrLine = strRdr.ReadLine()
Loop
strRdr.Close()

Survey1.txt sample:

RegID ItemName Response
214555134 NewRegID
214555134 1 Y
214555134 2 N
214555134 3 Y
214555134 4 1
214555134 5 Y
214555134 6 Y
214555134 7 Y
214555134 8 Y
214555134 9 1
214555134 10 Y
214555134 11 Y
214555134 12 Y
214555134 13 Y
214555134 14 Y
214555134 15 1
214555134 16
214555134 17 Y
214555134 18 Y
214555134 19 Y
214555134 20 Y
214555134 21 1
214555134 22 N
214555134 23 N
214555134 24 1
214555134 25 2
214016421 NewRegID
214016421 1 Y
214016421 2 Y
214016421 3 Y
214016421 4 1
214016421 5 Y
214016421 6
214016421 7 Y
214016421 8 Y
214016421 9 1
214016421 10 Y
214016421 11 N
214016421 12 Y
214016421 13 Y
214016421 14 Y
214016421 15 1
214016421 16 Y
214016421 17
214016421 18 Y
214016421 19 Y
214016421 20 Y
214016421 21 1
214016421 21 1
214016421 22 Y
214016421 23 N
214016421 24 2
214016421 25 3
213565432 1 Y
213565432 2 N
213565432 3 N
...
EOF

Any help would be greatly appreciated it.

Thanks!

解决方案

IL***@NETZERO.NET wrote:

Hi, I just started learning Visual Basic (VB.NET 03) and I need to do
this small program that will read a text file we get from another
company that has survey data, parse it and flatten it out and make
single strings out of the records in it. The major difference between
this space delimited file from the many examples in these groups is
that the data is not presented horizontally, but vertically. You can
see an example below.

What I need from this Visual Basic procedure to do first is to ignore
the first line of the text file (headings). Then, extract the RegID
from the first survey record, get all 25 numbers with their answers
(some of them are blank and some of them are numbers) and convert it
into a single string. After this is done, I need a carriage return so
the next survey record can be flatten out. One thing that might make
this easier is that I do get a line that says "NewRegID" when a new
survey record starts.

I need the final data to look like this:

214555134,1,Y,2,N,3,Y,4,1,5,Y,6,Y,7,Y,8,Y,9,1,10,Y ,11, ... all the way
to 25,2.
214016421,1,Y,2,Y,3,Y, .. and so on.

and it will be saved as a text file.

So far, this is what I have in my command button. This basically opens
the text file with StreamReader, reads it line by line and places it
in a text box called txtStrings. I''m missing the most important part:
the record builder and string creation.
Dim StrFileName As String
strFileName ="C:\Survey01.txt"
If Not System.IO.File.Exists(StrFileName) Then
MsgBox("File does not exists.")
Exit Sub
End If

Dim strRdr As System.IO.StreamReader =
System.IO.File.OpenText(StrFileName)

Dim StrLine As String
StrLine = strRdr.ReadLine()
Do Until StrLine Is Nothing
txtStrings.AppendText(StrLine & vbCrLf)
StrLine = strRdr.ReadLine()
Loop
strRdr.Close()

Survey1.txt sample:

RegID ItemName Response
214555134 NewRegID
214555134 1 Y
214555134 2 N
214555134 3 Y
214555134 4 1
214555134 5 Y
214555134 6 Y
214555134 7 Y
214555134 8 Y
214555134 9 1
214555134 10 Y
214555134 11 Y
214555134 12 Y
214555134 13 Y
214555134 14 Y
214555134 15 1
214555134 16
214555134 17 Y
214555134 18 Y
214555134 19 Y
214555134 20 Y
214555134 21 1
214555134 22 N
214555134 23 N
214555134 24 1
214555134 25 2
214016421 NewRegID
214016421 1 Y
214016421 2 Y
214016421 3 Y
214016421 4 1
214016421 5 Y
214016421 6
214016421 7 Y
214016421 8 Y
214016421 9 1
214016421 10 Y
214016421 11 N
214016421 12 Y
214016421 13 Y
214016421 14 Y
214016421 15 1
214016421 16 Y
214016421 17
214016421 18 Y
214016421 19 Y
214016421 20 Y
214016421 21 1
214016421 21 1
214016421 22 Y
214016421 23 N
214016421 24 2
214016421 25 3
213565432 1 Y
213565432 2 N
213565432 3 N
..
EOF

Any help would be greatly appreciated it.

Thanks!



Maybe this will help you. You probably want to use the String.Split
function to get the indvidual items.

Dim StrFileName As String
strFileName ="C:\Survey01.txt"
If Not System.IO.File.Exists(StrFileName) Then
MsgBox("File does not exists.")
Exit Sub
End If

Dim strRdr As System.IO.StreamReader =
System.IO.File.OpenText(StrFileName)
Dim StrLine As String
Dim StringArray() as String
Do strRdr.Peek <> -1
StrLine = strRdr.ReadLine()
if StrLine.EndsWith(NewRegID) then
''This is a new Record
StringArray = StrLine.Split(" ")
''StringArray(0) is your new ID
else
''This continuing record
StringArray = StrLine.Split(" ")
''String(0) has Column1
''String(1) has Column2
''String(2) has Column3 (if there is one)
for ii as integer = 1 to String.Getupperbound(0)
''Append your string here
next
end if
Loop
strRdr.Close()


Hi EOF

Do you mean something as (typed here not checked so watch typos or whatever)


Dim StrLine As String
StrLine = strRdr.ReadLine() dim sb as new text.stringbuilder(StrLine.Substring(0,9))Do Until StrLine Is Nothing dim fields() = StrLine.Split()
sb.append(fields(1) & fields(2)) StrLine = strRdr.ReadLine() if Is StrLine Not Nothing AndAlso
strline.indexof("NewRegID") > -1 then
strWrt.WriteLine(sb.ToString)
sb = new text.Stringbuilder(StrLine.Substring(0,9))
end if Loop
strRdr.Close()



I hope this helps,

Cor


Hi,

Another implementation :

===================
Private Sub ReadText()
Dim fsr As New FileStream("VertFile.txt", FileMode.Open,
FileAccess.Read)
Dim fsw As New FileStream("ResultFile.txt", FileMode.Create,
FileAccess.Write)
Dim sr As New StreamReader(fsr)
Dim sw As New StreamWriter(fsw)

Dim thisLine As String
Dim lineContents() As String

''Ignore the first line of the text file
sr.ReadLine()
While sr.Peek > -1
thisLine = sr.ReadLine()
lineContents = thisLine.Split(New Char() {" "c})
If lineContents(1).Trim = "NewRegID" Then
''We have a new record. Write current value of sb and Insert new
line.
If sb.Length > 0 Then
''Replace the last comma with a period.
sb.Replace(",", ".", sb.Length - 1, 1)
sw.WriteLine(sb.ToString())
End If
sb = New StringBuilder()
sb.Append(lineContents(0))
sb.Append(",")
Else
''This is the same record. Keep adding text
sb.Append(lineContents(1))
sb.Append(",")
If sb.Length > 2 Then
sb.Append(lineContents(2))
sb.Append(",")
End If
End If
End While
''Replace the last comma with a period.
sb.Replace(",", ".", sb.Length - 1, 1)
sw.WriteLine(sb.ToString())
sw.Flush()
''Clean up
sw.Close()
sr.Close()
fsr.Close()
fsw.Close()
End Sub
===================

Note that your third set of records (213565432) does not have the line
"NewRegID".
When that line is inserted in the sample text, then it works.

Regards,

Cerebrus.


这篇关于字符串构建器(解析垂直显示的记录)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆