简体   繁体   English

通过 pscustomobject 导出 csv 显示不正确的 powershell

[英]Export csv via pscustomobject displaying incorrectly powershell

I have the following code:我有以下代码:

    function realtest
    {
         $files = Get-ChildItem -Path 'D:\data\' -Filter *.csv
         $tester = [PSCustomObject]@{

         foreach ($file in $files)
         {
             $tempName = $file.BaseName
             $temp = Import-Csv $file
             $tester | Add-Member -MemberType NoteProperty -Name $tempName -Value $temp.$tempName
         }
         $tester
         $tester | Export-Csv "D:\result.csv" -NoTypeInformation
    }

I am trying to export a bunch of data to CSV however when it is display the data on csv it is shown as below我正在尝试将一堆数据导出到 CSV 但是当它在 csv 上显示数据时显示如下

"E0798T102","E0798T103"
"System.Object[]","System.Object[]"

but when i do it as a print on console it displays as the below但是当我在控制台上打印时,它显示如下

E0798T102       E0798T103
---------       ---------
{0, 0, 0, 0...} {0, 0, 0, 0...}

Ultimately, I want E0798T102 and E0798T103 as seperate columns in the result.csv最终,我希望 E0798T102 和 E0798T103 作为 result.csv 中的单独列

just to note, I will have 50 csv to loop through and each should display as its own column只是要注意,我将有 50 个 csv 循环遍历,每个都应显示为自己的列

Based on your snippet, this can be significantly simplified:根据您的代码段,这可以大大简化:

function Get-Csv {
    $col = foreach ($file in Get-ChildItem -Path D:\data -Filter *.csv) {
        $csv = Import-Csv -Path $file.FullName
        [pscustomobject]@{
            $csv.($file.BaseName) = $csv
        }
    }
    $col | Export-Csv D:\result.csv -NoTypeInformation

    return $col
}

However , a file seems like the wrong approach because you're trying to embed objects under a header.但是文件似乎是错误的方法,因为您试图在标题下嵌入对象 This doesn't really work in a tabular format as you only get one layer of depth.这在表格格式中并不真正起作用,因为您只能获得一层深度。 You should either expand all the properties on your objects, or use a different format that can represent depth, like .您应该扩展对象上的所有属性,或者使用可以表示深度的不同格式,例如

The reason for your formatting woes is due to how the serialization works.格式化问题的原因是序列化的工作方式。 You're getting a string representation of your objects.您将获得对象的字符串表示形式。

Converting to isn't difficult, you just trade your Export-Csv call:转换为并不困难,您只需交易您的Export-Csv调用:

$col | ConvertTo-Json -Depth 100 | Out-File -FilePath D:\result.json

Note: I specify -Depth 100 because the cmdlet's default Depth is 2 .注意:我指定-Depth 100是因为 cmdlet 的默认Depth2

Here is an incredibly inefficient answer to your question.对于您的问题,这是一个非常低效的答案。 If left as is, it assumes your CSV files already have a header with the CSV file basename:如果保持原样,则假定您的 CSV 文件已经有一个带有 CSV 文件基本名称的标题:

$CSVs = Get-ChildItem -path 'D:\data\' -filter "*.csv" -file
$headers = $CSVs.basename
$table = [System.Data.DataTable]::new("Files")
foreach ($header in $headers) {
    $table.Columns.Add($header) | out-null
}
foreach ($CSV in $CSVs) {
    #$contents = Import-Csv $CSV -Header $CSV.basename # If CSV has no header
    $contents = Import-Csv $CSV # If CSV contains header
    $rowNumber = 0
    foreach ($line in $Contents) {
        $rowcount = $table.rows.count
        if ($rowNumber -ge $rowCount) {
            $row = $table.NewRow()
            $row[$CSV.basename] = $line.$($CSV.basename)
            $table.Rows.Add($row)

        }
        else {
            $row = $table.rows[$rowNumber]
            $row[$CSV.basename] = $line.$($CSV.basename)

        }

        $rowNumber++
    }
}
$table | Export-Csv output.csv -NoTypeInformation   

You can uncomment the commented $contents line if your CSV files do not have a header.如果您的 CSV 文件没有标题,您可以取消注释$contents行的注释。 You will just have to comment out the next $contents variable assignment if you uncomment the first.如果您取消注释第一个,您只需要注释掉下一个$contents变量赋值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM