2

我有一些大型 csv 文件(每个 1.5gb),我需要在其中替换特定值。我目前使用的方法非常慢,我相当肯定应该有一种方法可以加快速度,但我只是没有足够的经验知道我应该做什么。这是我的第一篇文章,我尝试通过搜索找到相关的内容,但没有发现任何内容。任何帮助,将不胜感激。

我的另一个想法是将文件分成块,以便我可以将整个内容读入内存,在那里进行所有替换,然后输出到合并文件。我试过这个,但我这样做的方式实际上似乎比我目前的方法慢。

谢谢!

    Sub Main()
    Dim fName As String = "2009.csv"
    Dim wrtFile As String = "2009.1.csv"
    Dim lRead
    Dim lwrite As String
    Dim strRead As New System.IO.StreamReader(fName)
    Dim strWrite As New System.IO.StreamWriter(wrtFile)
    Dim bulkWrite As String

    bulkWrite = ""
    Do While strRead.Peek <> -1
        lRead = Split(strRead.ReadLine(), ",")
        If lRead(9) = "5MM+" Then lRead(9) = "5000000"
        If lRead(9) = "1MM+" Then lRead(9) = "1000000"

        lwrite = ""
        For i = LBound(lRead) To UBound(lRead)
            lwrite = lwrite & lRead(i) & ","
        Next
        strWrite.WriteLine(lwrite)
     Loop

    strRead.Close()
    strWrite.Close()
End Sub
4

1 回答 1

2

您正在拆分和合并,这可能需要一些时间。

为什么不只阅读文本行。然后用适当的值替换任何出现的“5MM+”和“1MM+”,然后写入该行。

 Do While ...
    s = strRead.ReadLine();
    s = s.Replace("5MM+", "5000000")
    s = s.Replace("1MM+", "1000000")
    strWrite(s);
 Loop
于 2011-06-04T01:43:07.183 回答