从 csv 文件中提取数据并保存到文本文件(不会使 RAM 过载)
Extract data from csv file and save to text file (without overloading your RAM)
我有这个巨大的提供商 IP 地址范围列表,以及有关地址总数和提供商名称的附加信息。
它看起来像这样:
2.160.0.0,2.175.255.255,1048576,28/09/10,Telekom Deutschland GmbH
2.200.0.0,2.207.255.255,524288,18/11/10,Vodafone GmbH
为了将它提供给另一个程序,我必须将它转换为一个简单的文本文件,其中包含一个基本的 ip 范围列表。像这样:
2.160.0.0-2.175.255.255
2.200.0.0-2.207.255.255
因此,由于文件非常大,问题是:
如何将这种 csv table 转换为基于 txt 的 ip 范围列表,而不同时将整个文件加载到 RAM 中?
好吧,这就是我想出的答案。纯 AutoHotkey,一次只将一行加载到 RAM 中,它确实非常快速和可靠。
;Set input and output files
inputFile := "log.csv"
outputFile := "out.txt"
;If the output file exists already, delete it before we start.
If FileExist(outputFile) {
FileDelete, %outputFile%
If ErrorLevel
MsgBox, Error: Can't access "%outputFile%"!
}
;Count the lines of our input file
Loop, Read, %inputFile%
lineCount := A_Index
;Parse the input file, filter it and output it to the output file
Loop, Read, %inputFile%, %outputFile%
{
currentLine := A_LoopReadLine
If (currentLine != "") { ;If the current line is not empty
currentLineArray := StrSplit(currentLine,",") ;Split the line by the comma
stringToWrite := currentLineArray[1] "-" currentLineArray[2] . "`r`n" ;generate a basic ip range line with a dash as a seperator
FileAppend, %stringToWrite% ;write it to our output file
;TrayTip, Writing to ip range file..., Line %A_Index%/%lineCount%
}
}
我有这个巨大的提供商 IP 地址范围列表,以及有关地址总数和提供商名称的附加信息。
它看起来像这样:
2.160.0.0,2.175.255.255,1048576,28/09/10,Telekom Deutschland GmbH
2.200.0.0,2.207.255.255,524288,18/11/10,Vodafone GmbH
为了将它提供给另一个程序,我必须将它转换为一个简单的文本文件,其中包含一个基本的 ip 范围列表。像这样:
2.160.0.0-2.175.255.255
2.200.0.0-2.207.255.255
因此,由于文件非常大,问题是:
如何将这种 csv table 转换为基于 txt 的 ip 范围列表,而不同时将整个文件加载到 RAM 中?
好吧,这就是我想出的答案。纯 AutoHotkey,一次只将一行加载到 RAM 中,它确实非常快速和可靠。
;Set input and output files
inputFile := "log.csv"
outputFile := "out.txt"
;If the output file exists already, delete it before we start.
If FileExist(outputFile) {
FileDelete, %outputFile%
If ErrorLevel
MsgBox, Error: Can't access "%outputFile%"!
}
;Count the lines of our input file
Loop, Read, %inputFile%
lineCount := A_Index
;Parse the input file, filter it and output it to the output file
Loop, Read, %inputFile%, %outputFile%
{
currentLine := A_LoopReadLine
If (currentLine != "") { ;If the current line is not empty
currentLineArray := StrSplit(currentLine,",") ;Split the line by the comma
stringToWrite := currentLineArray[1] "-" currentLineArray[2] . "`r`n" ;generate a basic ip range line with a dash as a seperator
FileAppend, %stringToWrite% ;write it to our output file
;TrayTip, Writing to ip range file..., Line %A_Index%/%lineCount%
}
}