1 15956. 12152. 0.0000 0.00 0.00 0.00
2 16015. 12152. 0.0000 0.00 0.00 0.00
3 15976. 12152. 0.0000 0.00 0.00 0.00
4 15996. 12152. 0.0000 0.00 0.00 0.00
5 16015. 12212. 0.0000 0.00 0.00 0.00
6 16015. 12172. 0.0000 0.00 0.00 0.00
7 16015. 12192. 0.0000 0.00 0.00 0.00
8 15956. 12212. 0.0000 0.00 0.00 0.00
9 15996. 12212. 0.0000 0.00 0.00 0.00
10 15976. 12212. 0.0000 0.00 0.00 0.00
11 15956. 12192. 0.0000 0.00 0.00 0.00
12 15956. 12172. 0.0000 0.00 0.00 0.00
13 15977. 12186. 0.0000 0.00 0.00 0.00
14 15983. 12202. 0.0000 0.00 0.00 0.00
15 15997. 12179. 0.0000 0.00 0.00 0.00
16 15980. 12165. 0.0000 0.00 0.00 0.00
17 15970. 12201. 0.0000 0.00 0.00 0.00
18 15997. 12197. 0.0000 0.00 0.00 0.00
19 16001. 12164. 0.0000 0.00 0.00 0.00
20 16015. 12152. 1682.9 0.00 0.00 0.00
NODE X Y Z THXY THYZ THZX
21 16015. 12152. 112.19 0.00 0.00 0.00
22 16015. 12152. 224.38 0.00 0.00 0.00
23 16015. 12152. 336.57 0.00 0.00 0.00
24 16015. 12152. 448.77 0.00 0.00 0.00
25 16015. 12152. 560.96 0.00 0.00 0.00
26 16015. 12152. 673.15 0.00 0.00 0.00
27 16015. 12152. 785.34 0.00 0.00 0.00
28 16015. 12152. 897.53 0.00 0.00 0.00
29 16015. 12152. 1009.7 0.00 0.00 0.00
30 16015. 12152. 1121.9 0.00 0.00 0.00
31 16015. 12152. 1234.1 0.00 0.00 0.00
32 16015. 12152. 1346.3 0.00 0.00 0.00
33 16015. 12152. 1458.5 0.00 0.00 0.00
34 16015. 12152. 1570.7 0.00 0.00 0.00
35 15956. 12152. 1687.3 0.00 0.00 0.00
36 15996. 12152. 1684.3 0.00 0.00 0.00
37 15976. 12152. 1685.8 0.00 0.00 0.00
38 15956. 12152. 1574.8 0.00 0.00 0.00
39 15956. 12152. 1462.3 0.00 0.00 0.00
40 15956. 12152. 1349.8 0.00 0.00 0.00
想用vb.net读取以上txt的数据,使得只读前四列,带字母的不要,如:
1 15956. 12152. 0.000
2 16015. 12152. 0.000
3 15976. 12152. 0.000
4 15996. 12152. 0.000
5 16015. 12212. 0.000
6 16015. 12172. 0.000
7 16015. 12192. 0.000
8 15956. 12212. 0.000
9 15996. 12212. 0.000
10 15976. 12212. 0.000
11 15956. 12192. 0.000
12 15956. 12172. 0.000
13 15977. 12186. 0.000
14 15983. 12202. 0.000
15 15997. 12179. 0.000
16 15980. 12165. 0.000
17 15970. 12201. 0.000
18 15997. 12197. 0.000
19 16001. 12164. 0.000
20 16015. 12152. 1682.9
最主要的也就是把每行带字母的去掉,希望朋友们指点下,不胜感激!
18 个解决方案
#1
使用ReadLine方法,代码
http://msdn.microsoft.com/zh-cn/library/db5x7c0d.aspx
http://msdn.microsoft.com/zh-cn/library/db5x7c0d.aspx
#2
我想要的是只读取前四项(每项之间是有空格的),不需要一正行都读下来!我的思路是这样的:每一行的数据都存储为一个二维数组,然后只读取前四组,然后再把多余的含有字母的那一行全部删掉!问题是怎么把带字母的删掉呢,是不是可以这样:判断第一行是不是字母,如果是字母的话就把改行删掉,不是的话读取前四个数组! 希望你能帮帮我,现在等着写论文呢,比较着急!谢谢!
#3
s = sr.ReadLine()赋值给s然后SPlit,取前4个
#4
麻烦您能说的详细点吗?我初学vb不是很了解,我也看了些关于readline以及split的说明。但具体的不知道怎么用!或是我能加你的qq聊吗,不胜感激!
#5
例子
Dim sr As System.IO.StreamReader = New System.IO.StreamReader("d:\xx.txt", Encoding.GetEncoding("GB2312"))
Dim str As String
Dim arr As String()
Dim xx As Int32
Do
str = sr.ReadLine()
If Not (str Is Nothing) Then
arr = System.Text.RegularExpressions.Regex.Split(str, " +")
If Int32.TryParse(arr(0), xx) Then
Console.WriteLine( arr(0) + " " + arr(1) + " " + arr(2) + " " + arr(3))
End If
End If
Loop Until str Is Nothing
sr.Close()
#6
Dim r As StreamReader
Dim strTemp As String
Dim a() As String
Dim b(1000, 1000) As String
Dim i As Integer
Dim j As Integer
Dim m, n As Integer
Dim c(1000, 1000) As String
j = 1
r = New StreamReader("d:/ansysgp.txt", System.Text.Encoding.GetEncoding("GB2312"))
Do While Not r.EndOfStream
strTemp = r.ReadLine
a = strTemp.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) '按空格拆分数据
For i = 0 To a.Length - 1 '从第一个数据开始导入二维数组
b(j, i) = a(i)
'Debug.Print(b(j, i - 1))
'TextBox4.Text &= b(j, i - 1) & vbCrLf
'TextBox4.Text = b(1, 6)
m = j
n = i
c(m, n) = b(j, i)
Next
j = j + 1
Loop
非常感谢您热情的回答。以上代码也可以实现该功能!但是现在还剩下个问题,我不知道怎么把含有字母的行去掉!我的思路是是否可以把所以的字母全部替换成空格,如果这样的话替换完之后该行是否还存在呢?希望您能再给点意见!不胜感激!
#7
再执行
c(m, n) = b(j, i)
#8
和tab 打出的空格是一样的吧··
#9
用了下很可以,就是我处理的数据比较多有100万行呢,数组超限了,上文的数组b和c出现一下情况,
Dim r As StreamReader
Dim strTemp As String
Dim a() As String
Dim b(1000000, 1000000) As Integer
Dim i As Integer
Dim j As Integer
Dim m, n As Integer
Dim c(1000000, 1000000) As Integer
Dim xx As Int32
。。。。
引发类型为“System.OutOfMemoryException”的异常。
希望您能指点下怎么解决呢?
Dim r As StreamReader
Dim strTemp As String
Dim a() As String
Dim b(1000000, 1000000) As Integer
Dim i As Integer
Dim j As Integer
Dim m, n As Integer
Dim c(1000000, 1000000) As Integer
Dim xx As Int32
。。。。
引发类型为“System.OutOfMemoryException”的异常。
希望您能指点下怎么解决呢?
#10
100万行。还是导入到数据库中好处理吧
#11
导入到哪个数据库啊?处理数据库还是用vb.net吗?您能说的详细点吗?
#12
是不是可以读取和写入的时候,每读取一行再写入一行,就不用定义这么大的数组了?但是我不知道怎么编程啊!
#13
#14
Dim target As String
Dim pattern As String
Dim result As String
'读取TXT数据
target = System.IO.File.ReadAllText("d:\xx.txt", System.Text.Encoding.Default)
'定义正则表达式
pattern = "^\d{1,}( \d{1,}[.]\d{0,}){3}"
'在读取到的数据中查找前四列数据
result = ""
For Each m As System.Text.RegularExpressions.Match In _
System.Text.RegularExpressions.Regex.Matches(target, pattern, RegexOptions.Multiline)
result &= m.Value & vbCrLf
Next m
MessageBox.Show(result)
#15
这个是可以判断起是否是数字的,但是他会把空格所在的行也当成数字了,不知道怎么解决呢,我现在其他问题都解决了,就是在写入数据的时候他会因为空格行的存在,把上面的数据写两遍,出现数据重复的情况。
#16
我已经解决这个问题了,代码贴出来共享下,希望对后来的人有帮助。分数还是给你吧。
Dim r As StreamReader
Dim strTemp As String
Dim a1() As String
Dim a2() As String
Dim b1(99, 99) As String
Dim b2(99, 99) As String
Dim i As Integer
Dim j As Integer
Dim n As Integer
Dim c1(99, 99) As String
Dim xx As Int32
Dim SaveFileName As String
Dim myStreamWriter As StreamWriter
SaveFileName = "d:/xx.txt"
myStreamWriter = File.CreateText(SaveFileName)
Dim MyOutput As String
MyOutput = "*GRIDPOINTS"
myStreamWriter.WriteLine(MyOutput)
r = New StreamReader("d:/xx.txt", System.Text.Encoding.GetEncoding("GB2312"))
Do While Not r.EndOfStream
strTemp = r.ReadLine
a1 = strTemp.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) '按空格拆分数据
For i = 0 To a1.Length - 1 '从第一个数据开始导入二维数组
b1(1, i) = a1(i)
If Int32.TryParse(a1(0), xx) Then’判断是否是数字格式
n = i
c1(1, n) = b1(1, i)
End If
Next
j = j + 1
If c1(1, 0) <> 0 Then
MyOutput = "G" & " " & c1(1, 0) & " " & c1(1, 1) & " " & c1(1, 2) & " " & c1(1, 3)'读取前四个数据
myStreamWriter.WriteLine(MyOutput)‘写入文件
End If
Loop
myStreamWriter.Close()
只是我还想再问一下怎么删除txt中的空格行啊,或是读到该空格行的时候不写入,关键是怎么判断改行是空格行的!
#17
Dim fileContents As String
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
strFileName = OpenFileDialog1.FileName
fileContents = My.Computer.FileSystem.ReadAllText(strFileName, System.Text.Encoding.GetEncoding("GB2312"))
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
Dim strReader As New StringReader(fileContents)
Dim arrText As New ArrayList() '数组
Dim sLine As String = ""
Do
sLine = strReader.ReadLine()
If Not sLine Is Nothing Then '删除空行
arrText.Add(sLine) '逐行存入数据
End If
Loop Until sLine Is Nothing
strReader.Close()
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
strFileName = OpenFileDialog1.FileName
fileContents = My.Computer.FileSystem.ReadAllText(strFileName, System.Text.Encoding.GetEncoding("GB2312"))
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
Dim strReader As New StringReader(fileContents)
Dim arrText As New ArrayList() '数组
Dim sLine As String = ""
Do
sLine = strReader.ReadLine()
If Not sLine Is Nothing Then '删除空行
arrText.Add(sLine) '逐行存入数据
End If
Loop Until sLine Is Nothing
strReader.Close()
#18
看一个问题。
#1
使用ReadLine方法,代码
http://msdn.microsoft.com/zh-cn/library/db5x7c0d.aspx
http://msdn.microsoft.com/zh-cn/library/db5x7c0d.aspx
#2
我想要的是只读取前四项(每项之间是有空格的),不需要一正行都读下来!我的思路是这样的:每一行的数据都存储为一个二维数组,然后只读取前四组,然后再把多余的含有字母的那一行全部删掉!问题是怎么把带字母的删掉呢,是不是可以这样:判断第一行是不是字母,如果是字母的话就把改行删掉,不是的话读取前四个数组! 希望你能帮帮我,现在等着写论文呢,比较着急!谢谢!
#3
s = sr.ReadLine()赋值给s然后SPlit,取前4个
#4
麻烦您能说的详细点吗?我初学vb不是很了解,我也看了些关于readline以及split的说明。但具体的不知道怎么用!或是我能加你的qq聊吗,不胜感激!
#5
例子
Dim sr As System.IO.StreamReader = New System.IO.StreamReader("d:\xx.txt", Encoding.GetEncoding("GB2312"))
Dim str As String
Dim arr As String()
Dim xx As Int32
Do
str = sr.ReadLine()
If Not (str Is Nothing) Then
arr = System.Text.RegularExpressions.Regex.Split(str, " +")
If Int32.TryParse(arr(0), xx) Then
Console.WriteLine( arr(0) + " " + arr(1) + " " + arr(2) + " " + arr(3))
End If
End If
Loop Until str Is Nothing
sr.Close()
#6
Dim r As StreamReader
Dim strTemp As String
Dim a() As String
Dim b(1000, 1000) As String
Dim i As Integer
Dim j As Integer
Dim m, n As Integer
Dim c(1000, 1000) As String
j = 1
r = New StreamReader("d:/ansysgp.txt", System.Text.Encoding.GetEncoding("GB2312"))
Do While Not r.EndOfStream
strTemp = r.ReadLine
a = strTemp.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) '按空格拆分数据
For i = 0 To a.Length - 1 '从第一个数据开始导入二维数组
b(j, i) = a(i)
'Debug.Print(b(j, i - 1))
'TextBox4.Text &= b(j, i - 1) & vbCrLf
'TextBox4.Text = b(1, 6)
m = j
n = i
c(m, n) = b(j, i)
Next
j = j + 1
Loop
非常感谢您热情的回答。以上代码也可以实现该功能!但是现在还剩下个问题,我不知道怎么把含有字母的行去掉!我的思路是是否可以把所以的字母全部替换成空格,如果这样的话替换完之后该行是否还存在呢?希望您能再给点意见!不胜感激!
#7
再执行
c(m, n) = b(j, i)
#8
和tab 打出的空格是一样的吧··
#9
用了下很可以,就是我处理的数据比较多有100万行呢,数组超限了,上文的数组b和c出现一下情况,
Dim r As StreamReader
Dim strTemp As String
Dim a() As String
Dim b(1000000, 1000000) As Integer
Dim i As Integer
Dim j As Integer
Dim m, n As Integer
Dim c(1000000, 1000000) As Integer
Dim xx As Int32
。。。。
引发类型为“System.OutOfMemoryException”的异常。
希望您能指点下怎么解决呢?
Dim r As StreamReader
Dim strTemp As String
Dim a() As String
Dim b(1000000, 1000000) As Integer
Dim i As Integer
Dim j As Integer
Dim m, n As Integer
Dim c(1000000, 1000000) As Integer
Dim xx As Int32
。。。。
引发类型为“System.OutOfMemoryException”的异常。
希望您能指点下怎么解决呢?
#10
100万行。还是导入到数据库中好处理吧
#11
导入到哪个数据库啊?处理数据库还是用vb.net吗?您能说的详细点吗?
#12
是不是可以读取和写入的时候,每读取一行再写入一行,就不用定义这么大的数组了?但是我不知道怎么编程啊!
#13
#14
Dim target As String
Dim pattern As String
Dim result As String
'读取TXT数据
target = System.IO.File.ReadAllText("d:\xx.txt", System.Text.Encoding.Default)
'定义正则表达式
pattern = "^\d{1,}( \d{1,}[.]\d{0,}){3}"
'在读取到的数据中查找前四列数据
result = ""
For Each m As System.Text.RegularExpressions.Match In _
System.Text.RegularExpressions.Regex.Matches(target, pattern, RegexOptions.Multiline)
result &= m.Value & vbCrLf
Next m
MessageBox.Show(result)
#15
这个是可以判断起是否是数字的,但是他会把空格所在的行也当成数字了,不知道怎么解决呢,我现在其他问题都解决了,就是在写入数据的时候他会因为空格行的存在,把上面的数据写两遍,出现数据重复的情况。
#16
我已经解决这个问题了,代码贴出来共享下,希望对后来的人有帮助。分数还是给你吧。
Dim r As StreamReader
Dim strTemp As String
Dim a1() As String
Dim a2() As String
Dim b1(99, 99) As String
Dim b2(99, 99) As String
Dim i As Integer
Dim j As Integer
Dim n As Integer
Dim c1(99, 99) As String
Dim xx As Int32
Dim SaveFileName As String
Dim myStreamWriter As StreamWriter
SaveFileName = "d:/xx.txt"
myStreamWriter = File.CreateText(SaveFileName)
Dim MyOutput As String
MyOutput = "*GRIDPOINTS"
myStreamWriter.WriteLine(MyOutput)
r = New StreamReader("d:/xx.txt", System.Text.Encoding.GetEncoding("GB2312"))
Do While Not r.EndOfStream
strTemp = r.ReadLine
a1 = strTemp.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) '按空格拆分数据
For i = 0 To a1.Length - 1 '从第一个数据开始导入二维数组
b1(1, i) = a1(i)
If Int32.TryParse(a1(0), xx) Then’判断是否是数字格式
n = i
c1(1, n) = b1(1, i)
End If
Next
j = j + 1
If c1(1, 0) <> 0 Then
MyOutput = "G" & " " & c1(1, 0) & " " & c1(1, 1) & " " & c1(1, 2) & " " & c1(1, 3)'读取前四个数据
myStreamWriter.WriteLine(MyOutput)‘写入文件
End If
Loop
myStreamWriter.Close()
只是我还想再问一下怎么删除txt中的空格行啊,或是读到该空格行的时候不写入,关键是怎么判断改行是空格行的!
#17
Dim fileContents As String
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
strFileName = OpenFileDialog1.FileName
fileContents = My.Computer.FileSystem.ReadAllText(strFileName, System.Text.Encoding.GetEncoding("GB2312"))
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
Dim strReader As New StringReader(fileContents)
Dim arrText As New ArrayList() '数组
Dim sLine As String = ""
Do
sLine = strReader.ReadLine()
If Not sLine Is Nothing Then '删除空行
arrText.Add(sLine) '逐行存入数据
End If
Loop Until sLine Is Nothing
strReader.Close()
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
strFileName = OpenFileDialog1.FileName
fileContents = My.Computer.FileSystem.ReadAllText(strFileName, System.Text.Encoding.GetEncoding("GB2312"))
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
Dim strReader As New StringReader(fileContents)
Dim arrText As New ArrayList() '数组
Dim sLine As String = ""
Do
sLine = strReader.ReadLine()
If Not sLine Is Nothing Then '删除空行
arrText.Add(sLine) '逐行存入数据
End If
Loop Until sLine Is Nothing
strReader.Close()
#18
看一个问题。