导入CSV并替换所有特殊字符(Umlauts)

时间:2019-07-08 14:17:32

标签: powershell

我正在尝试导入CSV,并且想要更改CSV中的所有特殊字符。 例如ö-> oe; ü-> ue;等等 然后将所有这些导出回csv

CSV示例的内容:

101|49|201907020|DE|bla|Schütze|Yayjay|||info|info2||||||||DE||||||||
101|49|201907020|DE|bla|Götther|Yayway|||info|info2||||||||DE||||||||
101|49|201907020|DE|bla|blub|Yayway|||info|info2||||||||DE||||||||

尝试了不同的方式。

  1. 使用“ -replace”命令

    $ line = $ line-替换'ü','ue'

  2. 使用“ .replace”命令

    $ line = $ line.Replace('ö','oe')。Replace('ä','ae')

-

$importfile = "Path to CSV.csv"
$exportfile = "Path to Export.csv"
$Content = import-csv $importfile -Encoding UTF8 -Header A

$Content | Foreach-Object { 

    foreach ($property in $_.PSObject.Properties)
    {

        [String]$line = $property.Value

        Write-host "Linie 1 $line" 
        $line = $line -replace 'ü', 'ue'

# Commented to see the different outputs
#$line1 = $line.Replace('ö','oe').Replace('ä','ae').Replace('ü','ue').Replace('ß','ss').Replace('Ö','Oe').Replace('Ü','Ue').Replace('Ä','Ae')
        Write-host "Linie 2 $line" 
    }
}
$Content 

它不会替换特殊字符。

如果我将$ property.value替换为类似“ Bla |ü|”的字符串该脚本将ü替换为ue。

2 个答案:

答案 0 :(得分:0)

乍看之下,您不必使用Import-Csv来处理文本文件。这取决于您正在执行的任务,但是在这种情况下使用Get-Content看起来更合适,因为它会将文件读取为字符串数组而不是动态对象数组。

还请记住,-replace运算符使用正则表达式作为模式。 您需要转义特殊符号并遵循regexp语法。 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-6

我能够使用其unicode码替换这些特殊符号。

\ uXXXX,其中XXXX-十六进制代码。

https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference

    $importfile = 'C:\TEMP\ps_1\src.txt'
    $Content = Get-Content $importfile
    $Content | ForEach-Object {
        foreach ($ch in $_.ToCharArray())
        {
            Write-Host "$($ch)[$([int]$ch)] " -NoNewline
        } 
        Write-Host

        Write-Host $_ -ForegroundColor Green
        Write-Host ($_ -replace 'ü', 'ue') -ForegroundColor Yellow # works if script and src.txt are saved in the same encoding
        Write-Host ($_ -replace "\u0413\u0458", 'ue') -ForegroundColor White # works if src.txt is in UTF8
        Write-Host ($_ -replace "\u00FC", 'ue') -ForegroundColor Gray # works if src.txt is in UTF8 with BOM
    }

UPD:

区分大小写的替换。 src.txt文件只有一行:

--oöo--OÖO--uüu--UÜU--aäa--AÄA--bßb
$importfile = 'C:\TEMP\ps_1\src.txt'
$Content = Get-Content $importfile

$replacePairs = @{
    "\u00FC" = 'ue'
    "\u00DC" = 'Ue'
    "\u00F6" = 'oe'
    "\u00D6" = "Oe"
    "\u00E4" = 'ae'
    "\u00C4" = 'Ae'
    "\u00DF" = 'ss'
}

$Content | ForEach-Object {
    Write-Host $_ -ForegroundColor Green
    $resultLine = $_
    foreach($pair in $replacePairs.GetEnumerator())
    {
        Write-Host "`t$($pair.Key) $([char][Convert]::ToInt32(($pair.Key.Substring(4)), 16)) --> $($pair.Value)" -ForegroundColor Gray
        $resultLine = $resultLine -creplace $pair.Key, $pair.Value
        Write-Host $resultLine -ForegroundColor Gray
    }
    Write-Host $resultLine -ForegroundColor Yellow    
}

答案 1 :(得分:0)

已编辑。这就是您所需要的。根据需要添加更多-replace。如果需要,设置内容可以保存到同一文件,但这似乎很冒险。除非输入文件是utf8nobom,否则无需指定编码。

(get-content file.csv) -replace 'ö','oe' -replace 'ü','ue' | set-content file2.csv
相关问题