gpt4 book ai didi

csv - 错误填充单元格 “Unable to index into an object of type”

转载 作者:行者123 更新时间:2023-12-02 23:43:21 25 4
gpt4 key购买 nike

我正在尝试计算文件的一些基本统计信息,然后将计算出的值移动到数组中,以便可以将它们全部一起导出为CSV。在我敲入代码的最后几行之前,该代码非常有用,这时它将引发错误:

Unable to index into an object of type System.Management.Automation.PSObject. 
At C:\debug.ps1:66 char:6
$FinalTable[ <<<< 0].VZA = $VZA

因此正是这些代码行引发了错误:
$FinalTable[0].VZA = $VZA
$FinalTable[0].VAZ = $VAZ
$FinalTable[0].STDDEV = $StandardDev |

我检查了一下,以 TempOutput2$i.csv格式导入的 $FinalTable文件包含了我要填充的所有字段。我不是在某个地方正确声明某些变量吗?或者我在哪里错了?

这是我的代码:
$i = 1

While ($i -le 211) {
# Set the variable to the filename with the iteration number
$filename = "c:\zMFM\z550Output\20dSummer\fixed20dSum550Output$i.csv"

# Check to see if that a file with $filename exists. If not, skip to the
# next iteration of $i. If so, run the code to collect the statistics
# for each variable and output them each to a different file
If (Test-Path $filename) {
#Calculate the Standard Deviation
#First get the average of the values in the column
$STDEVInputFile = Import-Csv $filename

#Find the average and count for column 'td'
$STDEVAVG = $STDEVInputFile | Measure-Object td -Average | Select Count, Average
$DevMath = 0

# Sum the squares of the differences between the mean and each value in the array
Foreach ($Y in $STDEVInputFile) {
$DevMath += [math]::pow(($Y.Average - $STDEVAVG.Average), 2)

#Divide by the number of samples minus one
$STDEV = [Math]::sqrt($DevMath / ($STDEVAVG.Count-1))

$StandardDev = $STDEV
}

#Calculate the basic statistics for column 'td' with the MEASURE-OBJECT cmdlet
$STATS = Import-Csv $Filename |
Measure-Object td -ave -max -min |
#Export the statistics as a CSV
Export-Csv -NoType "c:\zMFM\z550Output\20dSummer\tempstats$i.csv"

#Store the values that will go into the final table as variables
$GetColumns = Import-CSV $filename
$VZA = $GetColumns[0].VZA
$VAZ = $GetColumns[0].VAZ

#Append the standard deviation variable to the statistics table and add the value

Import-Csv "c:\zMFM\z550Output\20dSummer\tempstats$i.csv" |
#Add these fields (columns) to the TempStats file
#Then populate them using the $GetColumns variable values
Select-Object @{Name = "VZA"; Expression = {$_."VZA"}}, @{Name = "VAZ"; Expression = {$_."VAZ"}}, @{Name = "STDDEV"; Expression = {$_."STDDEV"}}, Count, Average, Maximum, Minimum, Property |
#Export this as TempOutput2$1.csv
Export-Csv -NoType "c:\zMFM\z550Output\20dSummer\TempOutput2$i.csv"

#Import it back in as ANOTHER variable
$FinalTable = Import-Csv "c:\zMFM\z550Output\20dSummer\TempOutput2$i.csv"

#Populate the fields with the variable values
$FinalTable[0].VZA = $VZA
$FinalTable[0].VAZ = $VAZ
$FinalTable[0].STDDEV = $StandardDev |
#Export the $STATS file containing everything you need in the correct folder
Export-Csv -NoType "c:\zMFM\z550Output\20dSummer\Statistics20dSum550.csv"
}
$i++
}

最佳答案

如果输入文件只有一个数据行,则除非您强制执行,否则输出不会成为数组。

$FinalTable = @(Import-Csv "c:\zMFM\z550Output\20dSummer\TempOutput2$i.csv")

另外,如果只需要用值填充(附加)列,为什么在添加列时还没有这样做呢?
Import-Csv "c:\zMFM\z550Output\20dSummer\tempstats$i.csv" |
Select-Object @{Name="VZA";Expression={$VZA}},
@{Name="VAZ";Expression={$VAZ}},
@{Name="STDDEV";Expression={$STDDEV}},
Count, Average, Maximum, Minimum, Property | ...

关于csv - 错误填充单元格 “Unable to index into an object of type”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34154458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com