错误填充单元格"无法索引到类型"

时间:2015-12-08 11:16:31

标签: csv powershell

我正在尝试计算文件的一些基本统计信息,然后将计算出的值移动到数组中,以便将它们全部导出为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++
}

1 个答案:

答案 0 :(得分:2)

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

$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 | ...