如何循环通过从PowerShell中的csv文件的一行 [英] how do I loop through a line from a csv file in powershell

查看:930
本文介绍了如何循环通过从PowerShell中的csv文件的一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在powershell中处理csv文件,但我不知道处理CSV文件中的列标题。

I want to process a csv file in powershell, but I don't know what the column headings in the CSV file will be when it is processed.

例如:

$path = "d:\scratch\export.csv"
$csv = Import-csv -path $path

foreach($line in $csv)
{ 
    foreach ($head in $line | get-member | where-object {$_.MemberType -eq "NoteProperty"} | select Definition)
    {
        #pseudocode...
        doSomething($head.columnName, $head.value)
    }

}

我循环通过csv文件中的行,获取列的名称和值?或者还有另一种方法,我应该这样做(像不使用Import-csv)?

How do I loop through the line in the csv file, getting the name of the column and the value? Or is there another way I should be doing this (like not using Import-csv)?

推荐答案

Import-Csv $path | Foreach-Object { 

    foreach ($property in $_.PSObject.Properties)
    {
        doSomething $property.Name, $property.Value
    } 

}

这篇关于如何循环通过从PowerShell中的csv文件的一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆