String Powershell,如何使用pscustomobject或ordered按列获取csv?
作为Powershell的初学者,我有一个无法解决的问题。我使用以下代码以字符串形式获得不同CSV文件的平均值:String Powershell,如何使用pscustomobject或ordered按列获取csv?,string,powershell,csv,filesystemobject,pscustomobject,String,Powershell,Csv,Filesystemobject,Pscustomobject,作为Powershell的初学者,我有一个无法解决的问题。我使用以下代码以字符串形式获得不同CSV文件的平均值: # recovery of the list of csv files present in the folder "folder_1". $files=(Get-ChildItem -path "C:\folder_1\" -Recurse -Include *.csv) #loop to read each file and ave
# recovery of the list of csv files present in the folder "folder_1".
$files=(Get-ChildItem -path "C:\folder_1\" -Recurse -Include *.csv)
#loop to read each file and average the variables "PRESS_CELL" and "PRESS_DELTA
foreach($file in $files){
$varTemp = @((Get-Content $file | ConvertFrom-Csv | Measure-Object "PRESS_CELL","PRESS_DELTA" -Average | Export-Csv -Path "C:\folder_1\Alias.csv" -NoTypeInformation -Append))
Write-Host $varTemp
}
#added an increment to sort the data (averages)
$vartemp2 = Import-CSV "C:\folder_1\Alias.csv" | Select *,LINENUMBER | ForEach-Object -Begin { $Line = 1 } {
$_.LineNumber = $Line++
$_
} | Export-CSV "C:\folder_1\Alias2.csv" -NoTypeInformation
Write-Host $vartemp2
# sort data by "property" and by "LINENUMBER"
$vartemp3 = Import-Csv "C:\folder_1\Alias2.csv"
$vartemp3 | % { $_.LINENUMBER = [int]$_.LINENUMBER }
$vartemp3 | Sort-Object -Property @{ Expression = 'Property'; Ascending = $true }, @{ Expression = 'LINENUMBER'; Ascending = $true } |
Format-Table -Property "Average","Property","LINENUMBER"
# groups the values obtained in a table/customobject and export it to csv by column ?
$fields = [ordered]@{
PRESS_CELL = $vartemp3 | ? property -like *PRESS_CELL* | Select -ExpandProperty Average
PRESS_DELTA = $vartemp3 | ? property -like *PRESS_DELTA* |Select -ExpandProperty Average
}
$fields | export-csv C:\1_ICOS_data\Alias4.csv -Append ##DOESN'T WORK
[result][1]
但是,我希望将结果导出为CSV表(每个结果1列,以PRESS_单元格和PRESS_DELTA作为标题)。使用管道时:
| export-csv C:\temp\alias.csv -Append
我得到一个结果:
“有序的”
“System.Collections.Specialized.OrderedDictionary+OrderedDictionaryKeyValueCollection”、“System.Collections.Specialized.OrderedDictionary+OrderedDictionaryKeyValueCollection”、“False”、“System.Object”、“False”
使用pscustomobject:
System.Object[],“System.Object[]”
添加-NoTypeInformation以导出csv命令,如下所示
$fields = [ordered]@{
PRESS_CELL = $vartemp3 | ? property -like *PRESS_CELL* | Select -ExpandProperty Average
PRESS_DELTA = $vartemp3 | ? property -like *PRESS_DELTA* |Select -ExpandProperty Average
}
#Or:
$fields = [pscustomobject]@{
PRESS_CELL = $vartemp3 | ? property -like *PRESS_CELL* | Select -ExpandProperty Average
PRESS_DELTA = $vartemp3 | ? property -like *PRESS_DELTA* |Select -ExpandProperty Average }
$fields |export-csv "c:\fields.csv" -Append -NoTypeInformation
它是否返回一个数组?您可能需要在导出之前枚举它以获取字符串。请尝试
…Average | out string
该Average
是列Average
中找到的所有值的数组。您想做什么?创建所有这些值的平均值,或者??您好,谢谢您的帮助。我是se请附上完整的代码。我们的想法是读取文件夹中的所有csv文件,计算每列每个文件的平均值,并将它们聚合到最终的csv文件中(每个文件在1分钟的时间步长中包含30分钟的数据,我想得到一个最终的csv文件,其中包含每半小时的平均值)感谢您的帮助!我获得了以下类型:$fields |获取成员类型名称:System.Collections.Specialized.OrderedDictionary或$fields |获取成员类型名称:System.Management.Automation.PSCustomObject名称成员类型定义-------PRESS_CELL NoteProperty Object[]PRESS_CELL=System.Object[]PRESS_DELTA NoteProperty Object[]PRESS_DELTA=System.Object[]我想我需要在导出之前获得这两个变量的数组??我希望csv文件中每列有$fields.PRESS_CELL和$fields_PRESS_DELTA。但是我的印象是“值”在powershell结果中以逗号分隔的字符串给出:Name PRESS_CELL Value{45,6,46,7,248,9}我想在我的csv中:按_单元格作为标题,每行45,6,46,7 et 248,9我尝试了-NoTypeInformation,但基本上不起作用我不想要$fields.PRESS_单元格和$fields.PRESS_DELTA by column在csv中,但当我在powershell中看到结果时,值都在一个字符串中,用“,”分隔的字符串是csv(逗号分隔值)当在excel中打开它时,您将看到它作为表格,您可以将其另存为excel工作表,但是如果您需要单元格和增量作为列名,则在power shell中创建表格是另一回事。我将准备表格并发送给itHello,是的,这正是我的问题,如果我在csv中打开它,这些值将在列中用逗号分隔,但是,我希望将它们作为行(以DELTA作为列名,如“转置”到excel)。提前感谢!