【问题标题】:PowerShell Error: Import-CSV Cannot Open FilePowerShell 错误:Import-CSV 无法打开文件
【发布时间】:2015-12-07 16:46:15
【问题描述】:

我的 PowerShell 代码触发了一些奇怪的错误代码,我已经彻底搜索了这些代码,但无济于事。

我正在尝试创建一个脚本来计算一些 CSV 文件的一些基本统计信息,在我尝试创建和填充新列之前,它似乎可以工作。

我得到的错误代码是:

Import-Csv:无法打开文件“C:\zMFM\Microsoft.Powershell.Commands.GenericMeasureInfo”。 在 C:\zMFM\StatsDebug.ps1:46 Char:21

+$STATS2 = 导入-CSV

+CategoryInfo : OpenError (:) [Import-Csv], FileNotFoundException

+FullyQualifiedErrorId : FileOpenFailure.Microsoft.Powershell.Commands.ImportCsvCommand 在 C:\zMFM\statsdebug.ps1:55 char:20

这之后是使用空表达式的错误,但我假设使用 Import-Csv 修复此问题将反过来修复该错误。这是我的代码,任何帮助都会很棒,谢谢!

$i = 1
#$colSTDDEV = New-Object System.Data.DataColumn StdDev,([double])
$colVZA = New-Object System.Data.DataColumn VZA,([double])
#$colVAZ = New-Object System.Data.DataColumn VAZ,([double])

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))

}

#Calculate the basic statistics for column 'td' with the MEASURE-OBJECT cmdlet
$STATS = Import-CSV $Filename |
Measure-Object td -ave -max -min

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

$STATS2 = Import-CSV $Stats
$VZA = $filename.VZA
#$VAZ = $filename.VAZ #COMMENTED FOR DEBUGING


#$STATS2.Columns.Add($colSTDDEV) #COMMENTED FOR DEBUGING
#$STATS2[0].StdDev = $STDEV #COMMENTED FOR DEBUGING


$STATS2.Columns.Add($colVZA)
$STATS2[0].VZA = $VZA


#$STATS2.Columns.Add($colVAZ) #COMMENTED FOR DEBUGING
#$STATS2[0].VZA = $VAZ #COMMENTED FOR DEBUGGING

#Export the $STATS file containing everything you need in the correct folder


Export-CSV -notype "c:\zMFM\z550Output\20dSummer\20dSum550Statistics.csv"

}
$i++
}

【问题讨论】:

    标签: csv powershell


    【解决方案1】:
    $STDEVInputFile = Import-CSV $filename
    ...
    $STATS = Import-CSV $Filename |
      Measure-Object td -ave -max -min
    # $STATS here will be type [GenericMeasureInfo], so you cannot use this as a filename.
    
    $STATS2 = Import-CSV $Stats
    # Are you trying to import $filename a third time here? You can't use $Stats because it's a [GenericMeasureInfo] object, not a [string].
    

    根据您的代码,您似乎正在尝试导入文件名类型为 [Microsoft.PowerShell.Commands.GenericMeasureInfo] 的 CSV。文件名是字符串。另外,你为什么要导入$filename 两次?建议导入一次,然后对保存到的变量进行操作。

    还建议将您的 while 循环更改为 1..211 | ForEach-Object。这样您就不必担心$i 是否小于或等于您的静态数字。没什么大不了的,但会使代码更具可读性。

    TL;DR

    Import-CSV $Stats 是问题所在。 $Stats 不是有效的文件名,因此您无法打开/导入它。

    【讨论】:

    • 哦,所以将“STATS”一词更改为其他内容应该可以解决问题吗?我会在几分钟后回到办公桌前试一试,并报告是否解决了问题。
    • 不,问题是您将Measure-Object td -ave -max -min 存储到变量$STATS 中,然后当它根本不是文件名时,您立即尝试使用$STATS 作为文件名打开CSV .
    • 当你通过管道传送到Measure-Object 时,结果是一个[GenericMeasureInfo] 类型的对象,你不能将它用作文件名。
    • 好的,我想我现在明白了。我没有立即尝试将 $STATS 视为 CSV,而是首先将其导出为 CSV 文件,然后重新导入,以便文件类型允许添加列。它似乎奏效了。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    相关资源
    最近更新 更多