如何获取 CSV 数据的列标题?
下面是具体数据:
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 的解决方案,而这里的上下文是:
描述
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.
也可以看看:
使用 PowerShell 从 CSV 进行对象关系映射? https://stackoverflow.com/q/64979287/4531180
如何将下载的 CSV 数据从 Invoke-RestMethod 传输到 Import-CSV? https://stackoverflow.com/q/64969211/4531180
以下伪代码片段旨在向您展示如何逐条记录地从 csv 文件中获取名称和值。外循环遍历记录,而内循环遍历当前记录中的列。
您可以调整它以满足您的需求。
Import-Csv yourfile.csv | % {
$_.psobject.properties | % {Set-variable -name $_.name -value $_.value}
{do stuff with local variables here..}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)