Count CSV columns without using Excel API

后端 未结 1 612
青春惊慌失措
青春惊慌失措 2021-01-24 15:57

Is there any method/code/reference to count the number of columns of every each row of records in a CSV file by maybe using PowerShell Get-Content and without using

相关标签:
1条回答
  • 2021-01-24 16:27

    If you Import-Csv, every item value ($_.PSObject.Properties.Value) is converted to a string, unless the whole item is missing from the column, the .Value property will be set to $Null.

    If your csv file includes a header row, it is presumed that the number of headers are at least the same as the maximum number of columns in a row:

    Import-Csv .\testing.csv | ForEach {@($_.PSObject.Properties | Where {$_.Value -ne $Null}).Count}
    

    In case your csv file doesn't include a header row, you might add a large number of headers yourself:

    Import-Csv .\testing.csv -Header @(0..99) | ForEach {@($_.PSObject.Properties | Where {$_.Value -ne $Null}).Count}
    
    0 讨论(0)
提交回复
热议问题