如何使用 Powershell 获取 CSV 数据的 headers 列?
how to get the column headers for CSV data with Powershell?
如何获取 CSV 数据的 headers 列?
具体数据如下:
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>
我试过:
$Csv.PSObject.Properties
$csv | Get-Member
两者似乎都没有提供特别有用的信息。理想情况下,结果将是用于迭代的数组或列表。
我见过适用于原始 CSV 的解决方案,而这里的上下文是:
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.
另见:
Object Relational Mapping from CSV with PowerShell?
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
**********************
出于问题的目的,只需查看 $labs[0] 即可很好地显示列。
以下伪代码片段旨在向您展示如何逐条记录地从 csv 文件中获取名称和值。外层循环遍历记录,而内层循环遍历当前记录中的列。
您可以根据自己的需要进行调整。
Import-Csv yourfile.csv | % {
$_.psobject.properties | % {Set-variable -name $_.name -value $_.value}
{do stuff with local variables here..}
}
如何获取 CSV 数据的 headers 列?
具体数据如下:
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>
我试过:
$Csv.PSObject.Properties
$csv | Get-Member
两者似乎都没有提供特别有用的信息。理想情况下,结果将是用于迭代的数组或列表。
我见过适用于原始 CSV 的解决方案,而这里的上下文是:
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.
另见:
Object Relational Mapping from CSV with PowerShell?
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
**********************
出于问题的目的,只需查看 $labs[0] 即可很好地显示列。
以下伪代码片段旨在向您展示如何逐条记录地从 csv 文件中获取名称和值。外层循环遍历记录,而内层循环遍历当前记录中的列。
您可以根据自己的需要进行调整。
Import-Csv yourfile.csv | % {
$_.psobject.properties | % {Set-variable -name $_.name -value $_.value}
{do stuff with local variables here..}
}