how to get the column headers for CSV data with Powershell?

帅比萌擦擦* 提交于 2021-02-11 15:49:13

问题


How can I get the column headers for CSV data?

Here is the specific data:

PS /home/nicholas/powershell/covid> 
PS /home/nicholas/powershell/covid> $labsURL='http://www.bccdc.ca/Health-Info-Site/Documents/BCCDC_COVID19_Dashboard_Lab_Information.csv'       
PS /home/nicholas/powershell/covid> 
PS /home/nicholas/powershell/covid> $labs_csv=Invoke-RestMethod -Method Get -Uri $labsURL -Headers @{"Content-Type" = "text/csv"}               
PS /home/nicholas/powershell/covid> 
PS /home/nicholas/powershell/covid> $labs = $labs_csv | ConvertFrom-Csv | Format-Table
PS /home/nicholas/powershell/covid> 

I've tried:

$Csv.PSObject.Properties   

$csv | Get-Member       

neither of which seem to give particularly useful information. Ideally, the result would be an array or list for iteration.

I've seen solutions which work with the raw CSV, whereas the context here being:

DESCRIPTION

The `ConvertFrom-Csv` cmdlet creates objects from CSV variable-length strings that are generated by the `ConvertTo-Csv`

cmdlet.

You can use the parameters of this cmdlet to specify the column header row, which determines the property names of the 
resulting objects, to specify the item delimiter, or to direct this cmdlet to use the list separator for the current culture 
as the delimiter.

The objects that `ConvertFrom-Csv` creates are CSV versions of the original objects. The property values of the CSV objects 
are string versions of the property values of the original objects. The CSV versions of the objects do not have any methods.

You can also use the `Export-Csv` and `Import-Csv` cmdlets to convert objects to CSV strings in a file (and back). These 
cmdlets are the same as the `ConvertTo-Csv` and `ConvertFrom-Csv` cmdlets, except that they save the CSV strings in a file.

see also:

Object Relational Mapping from CSV with PowerShell?

How to pipe downloaded CSV data from Invoke-RestMethod to Import-CSV?


回答1:


The following snatch of pseudo code is intended to show you how to pick up both the names and the values from a csv file, on a record by record basis. The outer loop iterates through the records, while the inner loop iterates through the columns in the current record.

You can adapt this to suit your needs.

   Import-Csv yourfile.csv | % {
       $_.psobject.properties | % {Set-variable -name $_.name -value $_.value}
       {do stuff with local variables here..}
   }



回答2:


transcript of solution from notjustme:

**********************
PowerShell transcript start
Start time: 20201215031406
Username: mordor\nicholas
RunAs User: mordor\nicholas
Configuration Name: 
Machine: mordor (Unix 5.4.0.56)
Host Application: /snap/powershell/149/opt/powershell/pwsh.dll
Process ID: 280596
PSVersion: 7.1.0
PSEdition: Core
GitCommitId: 7.1.0
OS: Linux 5.4.0-56-generic #62-Ubuntu SMP Mon Nov 23 19:20:19 UTC 2020
Platform: Unix
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.10032.0, 6.0.0, 6.1.0, 6.2.0, 7.0.0, 7.1.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
WSManStackVersion: 3.0
**********************
Transcript started, output file is transcript.labs.txt
PS /home/nicholas/powershell/covid> $labsURL='http://www.bccdc.ca/Health-Info-Site/Documents/BCCDC_COVID19_Dashboard_Lab_Information.csv'       
PS /home/nicholas/powershell/covid>  $labs_csv=Invoke-RestMethod -Method Get -Uri $labsURL -Headers @{"Content-Type" = "text/csv"}               
PS /home/nicholas/powershell/covid> $labs = $labs_csv | ConvertFrom-Csv
PS /home/nicholas/powershell/covid> $labs[0].psobject

BaseObject          :
Members             : {string Date=2020-01-23, string Region=BC, string New_Tests=2, string Total_Tests=2…}
Properties          : {string Date=2020-01-23, string Region=BC, string New_Tests=2, string Total_Tests=2…}
Methods             : {ToString, GetType, Equals, GetHashCode}
ImmediateBaseObject :
TypeNames           : {System.Management.Automation.PSCustomObject, System.Object}


PS /home/nicholas/powershell/covid> $labs[0]

Date        : 2020-01-23
Region      : BC
New_Tests   : 2
Total_Tests : 2
Positivity  : 0
Turn_Around : 32


PS /home/nicholas/powershell/covid> $labs[0].psobject.Properties.Name
Date
Region
New_Tests
Total_Tests
Positivity
Turn_Around
PS /home/nicholas/powershell/covid> Stop-Transcript
**********************
PowerShell transcript end
End time: 20201215031536
**********************

for the purposes of the question, just looking at $labs[0] nicely shows the columns.



来源:https://stackoverflow.com/questions/65304231/how-to-get-the-column-headers-for-csv-data-with-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!