VB.Net 替换大文本文件中的特定值 [英] VB.Net Replacing Specific Values in a Large Text File

查看:34
本文介绍了VB.Net 替换大文本文件中的特定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some large csv files (1.5gb each) where I need to replace specific values. The method I'm currently using is terribly slow and I'm fairly certain that there should be a way to speed this up but I'm just not experienced enough to know what I should be doing. This is my first post and I tried searching through to find something relevant but didn't come across anything. Any help would be appreciated.

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

My other thought would be to break the file into chunks so that I can read the entire thing into memory, do all of the replacements there and then output to a consolidated file. I tried this but the way I did it actually ended up seeming slower than my current method.

谢谢!

    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

推荐答案

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

You are splitting and the combining, which can take some time.

为什么不直接阅读文本行.然后用合适的值替换任何出现的5MM+"和1MM+",然后写下一行.

Why not just read the line of text. Then replace any occurance of "5MM+" and "1MM+" with the approiate value and then write the line.

 Do While ...
    s = strRead.ReadLine();
    s = s.Replace("5MM+", "5000000")
    s = s.Replace("1MM+", "1000000")
    strWrite(s);
 Loop

这篇关于VB.Net 替换大文本文件中的特定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆