线太多或太少

时间:2013-12-30 16:28:24

标签: arrays vb.net csv

下面是一段代码,它将数组重新编译为类似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

2 个答案:

答案 0 :(得分:0)

更改

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.

根据下面的评论,我相当肯定你的数组中有额外的字符会导致额外的行。要确认这一点,你可以通过手动删除像这样的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),"")

答案 1 :(得分:0)

为了加快你的代码,我会推荐一个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字符。