如何在powershell中循环遍历csv文件中的一行 [英] how do I loop through a line from a csv file in powershell

查看:118
本文介绍了如何在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:scratchexport.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天全站免登陆