【问题标题】:Too many or Too few line行太多或太少
【发布时间】:2014-01-17 13:41:19
【问题描述】:

下面是一段代码,它将数组重新编译为类似 csv 的文件。

Dim File As New System.IO.StreamWriter(varFileName)
    Dim NumberOfRows As Long = Array.GetUpperBound(0) ' Count number of rows.
    Dim NumberOfColumns As Long = Array.GetUpperBound(1) ' Count number of columns.

    Dim FileRows(NumberOfRows) As String
    File.Write("")
    'The idea here is to make up the row first by adding all of the columns together then write each row to the file.
    For i0 = 0 To NumberOfRows 'Loop through all of the Rows
        For i1 = 0 To NumberOfColumns 'Loop through all of the Columns
            If FileRows(i0) = "" Then 'Check if it is the first column then we dont need the ","
                FileRows(i0) = Array(i0, i1) 'If it is the first column we start the row with the first column
            Else
                FileRows(i0) = FileRows(i0) + ","c + Array(i0, i1) 'Now we take what we have so far and add "," and add the the next column
            End If
        Next ' We repeat this process untill of of the columns are written to a row.

        File.Write(FileRows(i0)) ' Now we write that row to the file.

    Next ' Move on to the next row.

    File.Close()

(请忽略我糟糕的编码习惯)

但是,如上所示,文件保存时没有新行,并且全部混合到一行中。

如果我将File.Write(FileRows(i0)) 行更改为File.WriteLine(FileRows(i0)),则会添加换行符,从而快速增加文件的大小。

我猜代码的第一个版本更正确,但我不确定如何解决缺少换行符的问题。任何帮助将不胜感激。 PS 稍微笨一点,我盯着这段代码看了两天,心都融化了。

将 csv 添加到数组代码中,以防出现问题。

 Dim FileData As New System.IO.StreamReader(varFileName)

    Dim FileRows() As String = FileData.ReadToEnd().Split(Environment.NewLine) 'Read in each row.
    Dim NumberOfRows As Long = FileRows.GetUpperBound(0) ' Count number of rows.

    Dim FileColumns() As String = FileRows(0).Split(","c) 'Split row 1 data in to columns.
    Dim NumberOfColumns As Long = FileColumns.GetUpperBound(0) ' Count number of columns.

    ReDim dataArray(NumberOfRows, NumberOfColumns) 'Declare the array correctly with new data

    For x = 0 To NumberOfRows 'Cycle through all the rows
        FileColumns = FileRows(x).Split(","c) 'Split row into columns
        For y = 0 To NumberOfColumns 'Now cycle through all the columns on that row
            Try
                dataArray(x, y) = FileColumns(y) 'Set each piece of data into the array.
            Catch ex As Exception
            End Try
        Next
    Next

    FileData.Close() 'This piece of code takes the csv file and turns it into an array. The data can now be called using Array(1,1) to get the first value

【问题讨论】:

  • 您试图在不增加文件大小的情况下添加换行符?那是不可能的。但它不应该真的导致这么大的尺寸增加。
  • 不幸的是,从文件中检索代码无法处理额外的行距

标签: arrays vb.net csv


【解决方案1】:

改变

File.Write(FileRows(i0)) ' Now we write that row to the file.

到

File.WriteLine(FileRows(i0)) ' Now we write that row to the file.

试试这个,摆脱你的 FileRows(你不需要保持你的线条正确吗?)

For i0 = 0 To NumberOfRows 'Loop through all of the Rows
    Dim Line as string = nothing
    For i1 = 0 To NumberOfColumns 'Loop through all of the Columns
        If line is nothing Then 'Check if it is the first column then we dont need the ","
            line = Array(i0, i1) 'If it is the first column we start the row with the first column
        Else
            line &= ","c + Array(i0, i1) 'Now we take what we have so far and add "," and add the the next column
        End If
    Next ' We repeat this process until all of the columns are written to a row.

    File.WriteLine(line) ' Now we write that row to the file.

Next ' Move on to the next row.

根据下面的 cmets,我相当肯定您的数组中有额外的字符导致额外的行。要确认这一点,您可以通过使用这样的 REGEX 手动删除非标准字符来运行(这不是功能代码,而是一种证明额外字符存在的方法)

Public nonAplaNum As New Regex("[^a-z_0-9]", RegexOptions.Compiled Or RegexOptions.IgnoreCase Or RegexOptions.ExplicitCapture)

' wrap each array assignment with this ...
line &= ","c + nonAlphaNum.replace(Array(i0, i1),"")

【讨论】:

  • 我看到了你关于已经这样做的评论,但如果你需要换行,你别无选择。
  • 问题是当我这样做时,随机换行符出现在我的文件中。
  • 除了缺少的“WriteLine”之外,我没有看到您的初始代码有任何实际问题。您确定您查看的是正确的文件输出而不是旧文件输出(或者可能正在附加到旧文件输出)
  • 唯一的另一件事可能是您的数据中有换行符(但您也会看到整个中断)
  • 这几乎奏效了。现在我在每条数据之后都有一个常规的额外行
【解决方案2】:

只是为了加快您的代码速度,我建议您使用StringBuilder(尽管我知道还有另外 101 种剥猫皮的方法)...

Dim File As New System.IO.StreamWriter(varFileName)
Dim rows As Long = Array.GetUpperBound(0) ' Count number of rows.
Dim cols As Long = Array.GetUpperBound(1) ' Count number of columns.

For row As Integer = 0 To rows 'Loop through all of the Rows
    For col = 0 To cols 'Loop through all of the Columns
        Dim sb As New StringBuilder()
        sb.Append(Array(col, row)
        If col < cols Then sb.Append(",")
    Next col ' We repeat this process untill of of the columns are written to a row.

    If sb.ToString().InexOf(vbCrLf) > 0 Then sb.Replace(vbCrLf, "")
    File.WriteLine(sb.ToString()) ' Now we write that row to the file.

Next row ' Move on to the next row.

File.Close()

我建议检查您的阵列,看看是否有任何恶意 CR/LF 或其他导致问题的破坏字符。

-- 编辑--

添加了用于检查和替换 CrLf 字符的新部分。

【讨论】:

  • 关于如何检查数组的任何指针。对 VB 相当陌生,从未享受过多维数组
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-29
  • 1970-01-01
  • 2010-12-01
相关资源
最近更新 更多