0

I have a hundreds of space delimited text files that I would like to convert in VB.NET to CSV-files according to RFC 4180. The files have variying length but can contain upp to 200000 rows and have varying ammount of columns (up to 30) separated by a number of "spaces" that also varies between one and 20. The files contains some information that I would like to remove, for example the first column and I would also like to change the content of the second column into a valid Javascript time stamp. This is an exemple of the first rows of one of the files (note that most of the spaces are not shown).

# time order boil_q_1 boil_q_2 chil_q1 chil_q2 loccool locheat qdomwat
0.000000000 1.0000 4.18700E-09 0.0000 4.18700E-11 1.31529E-03 132.39 9799.3 0.0000
8.0000000000E-02 1.0000 4.18700E-09 0.0000 4.18700E-11 1.31528E-03 132.11 9917.1 0.0000
0.1600000000 1.0000 4.18700E-09 0.0000 4.18700E-11 1.31527E-03 131.98 10047. 0.0000
0.2705515735 1.0000 4.18700E-09 0.0000 4.18700E-11 1.31527E-03 131.97 10152. 0.0000
0.2705515763 1.0000 4.18700E-09 0.0000 4.18700E-11 1.31526E-03 131.97 10152. 0.0000
0.3345515763 1.0000 4.18700E-09 0.0000 4.18700E-11 1.31525E-03 131.97 10184. 0.0000
0.3985515763 1.0000 4.18700E-09 0.0000 4.18700E-11 1.31524E-03 131.98 10192. 0.0000
0.5265515763 1.0000 4.18700E-09 0.0000 4.18700E-11 1.31524E-03 131.98 10178. 0.0000
0.7825515763 1.0000 4.18700E-09 0.0000 4.18700E-11 1.31523E-03 131.99 10164. 0.0000
0.7825515791 1.0000 4.18700E-09 0.0000 4.18700E-11 1.31522E-03 131.99 10164. 0.0000

Any suggenstions would be wellcome.

Sincerely Max

4

2 回答 2

0

我最终使用以下代码并将信息添加到数据网格

Dim rowvalue As String
Dim streamReader As IO.StreamReader = New IO.StreamReader(FileName)
    While streamReader.Peek() <> -1
    rowvalue = streamReader.ReadLine()
    DataGridView1.Rows.Add(rowvalue.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries))
End While

问题是它相当慢,我仍然需要删除其中一个文件并添加所有其他文件。我想以其他方式存储信息会更容易吗?有任何想法吗?

于 2013-06-14T06:42:28.073 回答
0

假设不同数量的空格不是因为列是空的......

我只是保持简单并使用我习惯的字符串工具,对我来说很粗糙但很快:

    Dim s As String = TextBox1.Text
    s = s.Replace(vbNewLine, "|").Replace(vbCr, "").Replace(vbLf, "")
    s = s.Replace("| ", "|") ' trim leading space
    Dim iLen As Integer = -1
    Do
        iLen = s.Length
        s = s.Replace("  ", " ")
    Loop Until iLen = s.Length
    Dim aLines() As String = s.Split("|")
    Dim aLine() As String
    Dim aHeader() As String = aLines(0).Split(" ")
    ' process header
    For i As Long = 1 To aLines.GetUpperBound(0)
        aLine = aLines(i).Split(" ")
        Stop
        ' process line
    Next

TextBox1 包含您的样品与流浪?领导 #。如果不同的空间是由于空列造成的,则按摩将更加复杂,并且可能在内部循环中更好地完成。

上面我只是按摩整个字符串,然后将线条放入一个数组中,然后遍历线条。

这是 OP 中唯一的第一步。

我发现编写 CSV 是有问题的,尤其是因为消费者有时对规范所说的内容有不同的想法。如果可能,请使用最终消耗程序进行测试。

于 2013-06-10T16:21:04.263 回答