我有265个CSV文件,总记录(行数)超过400万,需要在所有CSV文件中进行搜索和替换.我在下面有一段我的PowerShell代码可以执行此操作,但执行操作需要17分钟:
ForEach ($file in Get-ChildItem C:\temp\csv\*.csv)
{
$content = Get-Content -path $file
$content | foreach {$_ -replace $SearchStr, $ReplaceStr} | Set-Content $file
}
Run Code Online (Sandbox Code Playgroud)
现在我有以下Python代码执行相同的操作,但执行时间不到1分钟:
import os, fnmatch
def findReplace(directory, find, replace, filePattern):
for path, dirs, files in os.walk(os.path.abspath(directory)):
for filename in fnmatch.filter(files, filePattern):
filepath = os.path.join(path, filename)
with open(filepath) as f:
s = f.read()
s = s.replace(find, replace)
with open(filepath, "w") as f:
f.write(s)
findReplace("c:/temp/csv", "Search String", "Replace String", "*.csv")
Run Code Online (Sandbox Code Playgroud)
为什么Python方法效率更高?我的PowerShell代码是无效的,还是Python在文本操作方面只是一种更强大的编程语言?
使用PowerShell 2.0:
write-output "abcd" >> mytext.txt
Run Code Online (Sandbox Code Playgroud)
收益:
n n b nul c nul d nul
od -c将nul显示为真正的二进制零\0,或:( a \0 b \0 c \0 d \0和\r \0 \n \0).
我试图生成一些SQL,所以我不认为这样做.有关正在发生什么的想法以及如何使用write-output来获取指定的字符?