相关疑难解决方法(0)

在大型搜索/替换操作中,PowerShell很慢(比Python慢​​得多)?

我有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在文本操作方面只是一种更强大的编程语言?

python powershell performance replace

21
推荐指数
2
解决办法
1万
查看次数

Powershell 2.0在字符之间生成空值

使用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来获取指定的字符?

null powershell-2.0

14
推荐指数
1
解决办法
5711
查看次数

标签 统计

null ×1

performance ×1

powershell ×1

powershell-2.0 ×1

python ×1

replace ×1