我正在创建一个脚本来解析 CSV 文件,其中我将 CSV 中每个索引字段的内容存储为 PSCustomObject 中的 NoteProperty。
当我逐行解析文件时,我将 PSCustomObject 添加到列表类型。当我输出我的列表时,我希望能够执行以下操作:
$list | Format-Table
并且可以很好地查看 csv 文件中的每一行,并分成标题向上的列。
问题
当我将 PSCustomObject 添加到列表时,它会将列表的类型更改为 PSCustomObject。在实践中,这具有将对该 PSCustomObject 所做的任何更新追溯应用到列表中的每个元素的明显效果。
这是一个示例:
$list = [System.Collections.Generic.List[object]]::new()
$PSCustomObject = [PSCustomObject]@{ count = 0}
Foreach ($i in 1..5) {
$PSCustomObject.count +=1
$list.Add($PSCustomObject)
}
预期输出:
PS>$list
count
-----
1
2
3
4
5
实际输出:
PS>$list
count
-----
5
5
5
5
5
问题
有没有办法获得预期的输出?
限制/附加上下文(如果有帮助)
我正在尝试优化性能,因为我可能会解析非常大的 CSV 文件。这就是为什么我被一个列表困住了。我了解列表中的 Add 方法比使用 += 为每一行重新创建数组更快。我还使用运行空间池分别解析每个字段并通过更新对象$list.$field[$lineNumber] = <field value>
,所以这就是为什么我需要一种动态更新 PSCustomObject 的方法。我的代码的更大视图是:
$out = [hashtable]::Synchronized(@{})
$out.Add($key, @{'dataSets' = [List[object]]::new() } ) ### $key is the file name as I loop through each csv in a directory.
$rowTemplate = [PSCustomObject]@{rowNum = 0}
### Additional steps to prepare the $out dictionary and some other variables
...
...
try {
### Skip lines prior to the line with the headers
$fileParser = [System.IO.StreamReader]$path
Foreach ( $i in 1..$headerLineNumber ) {
[void]$fileParser.ReadLine()
}
### Load the file into a variable, and add empty PSCustomObjects for each line as a placeholder.
while ($null -ne ($line = $fileParser.ReadLine())) {
[void]$fileContents.Add($line)
$rowTemplate.RowNum += 1
[void]$out.$key.dataSets.Add($rowTemplate)
}
}
finally {$fileParser.close(); $fileParser.dispose()}
### Prepare the script block for each runspace
$runspaceScript = {
Param( $fileContents, $column, $columnIndex, $delimiter, $key, $out )
$columnValues = [System.Collections.ArrayList]::new()
$linecount = 0
Foreach ( $line in $fileContents) {
$entry = $line.split($delimiter)[$columnIndex]
$out.$key.dataSets[$linecount].$column = $entry
$linecount += 1
}
}
### Instantiate the runspace pool.
PS 版 (5.1.19041)