我有一个名为 Volume 的 csv 列,我正在尝试将其重新格式化为给定的小数位数。这是我从 ChatGPT 获得的信息,但出现错误: 无法处理该属性,因为属性“Volume”已存在。
$data = Import-Csv $AF
$columnName = "Volume"
$numberOfDecimalPlaces = 2
# Format the column with the specified number of decimal places
$newData = $data | Select-Object *,@{
Name = $columnName
Expression = { "{0:N$numberOfDecimalPlaces}" -f [decimal]$_.$columnName }
}
$newData | Export-Csv $AF -NoTypeInformation
任何有关格式化该列的帮助将不胜感激。我有很多要格式化。
通过使用
*,@{Name='Volume', ...}
,您告诉它使用“所有列,包括交易量,然后还添加另一个‘交易量’列”
# Load CSV file in a collection
$Csv = Import-Csv $File
# self-loop through the collection, changing the value as you need
# BEWARE: the decimal separator will be in the style of the [cultureinfo]::currentculture value
$Csv.Foreach({ $_.$Column= "{0:N$numberOfDecimalPlaces}" -f [decimal]$_.$Column})
# export back the collection
# you can skip -NoTypeInformation if you are using Powershell 6+
$Csv | Export-Csv -Path $File -NoTypeInformation