我的 CSV 包含以下数据:
纯文本格式的 CSV:
System Speed Dialling Number,"Name (20 characters)","CO Line Access Number + Telephone Number (32 digits)","CLI Destination"
0,"USER1","Dial=00123456300","Dial="
1,"USER2","Dial=00123456400","Dial="
2,"","",""
3,"","",""
4,"","",""
5,"","",""
6,"","",""
7,"","",""
8,"","",""
9,"","",""
10,"","",""
最多 1000 行。
我尝试使用以下脚本替换“CO 线路接入号码 + 电话号码(32 位)”和“CLI 目标”中的空值栏目:
$dial1 = import-csv C:\TEMP\dial.csv
$dial1 | foreach-object {
$_.'CO Line Access Number + Telephone Number (32 digits)' = $_.'CO Line Access Number + Telephone Number (32 digits)'.replace("","Dial=")
}
$dial1 | export-csv C:\TEMP\dial.csv –NoTypeInformation
错误:字符串长度不能为零
问题:有没有办法使用脚本和值“Dial=”填充提到的列
抱歉,我没有这方面的经验...
或者,我想到了一个脚本,该脚本仅将用户的行从一个 CSV 写入另一个 CSV(例如,其中“名称(20 个字符)”不为空)。不幸的是我找不到任何关于此的信息。
最佳答案
您可以按照此逻辑来完成此操作,使用 String.IsNullOrWhiteSpace
检查CO Line Access Number + Telephone Number(32 位)
列上的值是否为空或空白字符串。 ,如果条件
计算结果为 $true
,将所需值 (Dial=
) 重新分配给该属性。
Import-Csv path\to\csv.csv | ForEach-Object {
if([string]::IsNullOrWhiteSpace($_.'CO Line Access Number + Telephone Number (32 digits)')) {
$_.'CO Line Access Number + Telephone Number (32 digits)' = 'Dial='
}
$_
} | Export-Csv newfixedcsv.csv -NoTypeInformation
为了简要解释该错误,.Replace($oldValue, $newValue)
提示,因为 $oldValue
的参数不能是空字符串(''
或 [string]::Empty
或 [NullString]::Value
)。
关于powershell - 使用 PowerShell 替换 CSV 中的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73816805/