PowerShell - 如何将导入的列视为一组数字?
PowerShell - How to treat an imported column as a set of numeric digits?
我有一个类似于以下内容的 CSV:
No,BundleNo,Grossweight,Pieces,Tareweight,Netweight
1,Q9,193700,1614,646,193054
2,Q7,206400,1720,688,205712
我想做的是将 Netweight
列中的每个值除以 25,但由于某种原因我正在努力。
这是我的代码:
$fileContent = Import-Csv $file
$netWeight = $fileContent | select Netweight
$netWeight | Foreach-Object { $_/25 }
这给我一个错误:
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Division'.
我尝试了多种尝试将值转换为数值但没有成功。
感谢任何帮助。
谢谢,
约翰
您有几个选择,问题是您正在尝试对对象的 属性 进行操作。
使用您当前的版本,您仍然需要显式访问 Netweight
属性,即使您已经 select 编辑了它:
$netWeight = $fileContent | select Netweight
$netWeight | Foreach-Object { $_.Netweight / 25 }
或者,您可以在 select 时将 属性 展平:
$netWeight = $fileContent | select -ExpandProperty Netweight
$netWeight | Foreach-Object { $_/25 }
我有一个类似于以下内容的 CSV:
No,BundleNo,Grossweight,Pieces,Tareweight,Netweight
1,Q9,193700,1614,646,193054
2,Q7,206400,1720,688,205712
我想做的是将 Netweight
列中的每个值除以 25,但由于某种原因我正在努力。
这是我的代码:
$fileContent = Import-Csv $file
$netWeight = $fileContent | select Netweight
$netWeight | Foreach-Object { $_/25 }
这给我一个错误:
Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Division'.
我尝试了多种尝试将值转换为数值但没有成功。
感谢任何帮助。 谢谢, 约翰
您有几个选择,问题是您正在尝试对对象的 属性 进行操作。
使用您当前的版本,您仍然需要显式访问 Netweight
属性,即使您已经 select 编辑了它:
$netWeight = $fileContent | select Netweight
$netWeight | Foreach-Object { $_.Netweight / 25 }
或者,您可以在 select 时将 属性 展平:
$netWeight = $fileContent | select -ExpandProperty Netweight
$netWeight | Foreach-Object { $_/25 }