我有一个包含某些列的CSV文件。在这些列中,某些字符是德语。
样本数据
Test01ñ
DúSibaagh01
ËTheroË01Ë
DMrçzundaljak01
PçSchpaglawarz01ç
脚本
import-csv D:\users.csv |
foreach {
If($_.Samaccountname -contains "ñ") {
$_.Samaccountname -replace "ñ","N"
}
} | export-csv D:\Users_myfile.csv
不幸的是,脚本没有将ñ替换为 N 如果条件始终为假。 我尝试使用-match和-like关键字。但是在这种情况下,它们都不起作用。
任何人都建议该怎么做。
答案 0 :(得分:0)
-contains
是一个集合运算符-它不适用于字符串。请改用String.Contains()
:
Import-Csv D:\users.csv |ForEach-Object {
if($_.Samaccountname.Contains("ñ")) {
$_.Samaccountname = $_.Samaccountname -replace "ñ","N"
}
$_
} |Export-Csv D:\Users_myfile.csv
答案 1 :(得分:0)
-Encoding UTF8
Remove-Diacritics
函数。给出一个示例users.csv:
"UserName","LastName"
"Test01ñ","Test01ñ"
"DúSibaagh01","DúSibaagh01"
"ËTheroË01Ë","ËTheroË01Ë"
"DMrçzundaljak01","DMrçzundaljak01"
"PçSchpaglawarz01ç","PçSchpaglawarz01ç"
此脚本:
## Q:\Test\2018\12\24\SO_53912246.ps1
function Remove-Diacritics {
param ([String]$src = [String]::Empty)
# Source: https://stackoverflow.com/a/7840951/6811411
$normalized = $src.Normalize( [Text.NormalizationForm]::FormD )
$sb = new-object Text.StringBuilder
$normalized.ToCharArray() | % {
if( [Globalization.CharUnicodeInfo]::GetUnicodeCategory($_) -ne
[Globalization.UnicodeCategory]::NonSpacingMark ) {
[void]$sb.Append($_)
}
}
$sb.ToString()
}
$CsvData = Import-csv .\Users.csv -Encoding UTF8
$CsvData | ForEach-Object {
$_.UserName = Remove-Diacritics $_.UserName
}
$CsvData
$CsvData | Export-Csv .\New_Users.csv -Encoding UTF8 -NoTypeInformation
将创建以下输出:
UserName LastName
-------- --------
Test01n Test01ñ
DuSibaagh01 DúSibaagh01
ETheroE01E ËTheroË01Ë
DMrczundaljak01 DMrçzundaljak01
PcSchpaglawarz01c PçSchpaglawarz01ç