2017-01-20 12 views
0

不正な値の行を削除するプログラムを作成しています。列名はあらかじめわかっていないので、ハードコーディングされていないことが重要です。私は、データ構造のインデックスとして列ヘッダーを使用してCSVファイルを読み取ろうとしています。 MemberTypeプロパティを使用してCSVプロパティを保存します。これで私は名前を得ることができますが、価値は得られません!列ヘッダーとPSMemberプロパティからCSVで行の値を見つけよう

here is the column name: car 
Here is the set of all values in [column, row]:#??? 
Here is the set of all values to purge: robert 
Here is the set of all values to purge: nissan exterra 
here is the column name: name 
Here is the set of all values in [column, row]:#??? 
Here is the set of all values to purge: robert 
Here is the set of all values to purge: nissan exterra 
here is the column name: car 
Here is the set of all values in [column, row]:#??? 
Here is the set of all values to purge: robert 
Here is the set of all values to purge: nissan exterra 
here is the column name: name 
Here is the set of all values in [column, row]:#??? 
Here is the set of all values to purge: robert 
Here is the set of all values to purge: nissan exterra 

私のデータは次のようにCSVに保存されています:パージへ

#ps_test.csv 
name car 
---- ------- 
clay nissan exterra 
casey honda accord 
henry nissan exterra 
robert truck 

項目

$data = .\hash_file.ps1 
$csv = Import-Csv .\ps_test.csv 

$properties = $csv | ForEach-Object { 
    $_ | Get-Member -MemberType Property, NoteProperty 
} 

foreach ($property in $properties) { 
    "here is the column name: " + $property.Name 
    "Here is the set of all values in [column, row]: " + $csv[$property.Name] #this does not return 
    foreach ($key in $data.Keys) { 
     "Here is the set of all values to purge: " + $data[$key] 
     if ($property.Name -eq $key -And $data[$key] -contains $property.Value){ 
      "Here is the purged property: " + $property.Value 
     } 
    } 
} 

そして、ここでは私の結果である:ここでは

は私のコードですハッシュテーブルに列、値のペアとして格納されます。

ここで

#hash_file.ps1 
@{ 
    "name" = @("robert"); 
    "car" = @("nissan exterra") 
} 
は例の結果である:

Here is the column name: name 
Here is the set of all values in [column, row]:#??? 
Here is the set of all values to purge: robert 
Here is the set of all values to purge: nissan exterra 

それは "名前"、 "粘土" を読むべき、 "ロバート"、 "日産exterra"。しかし、私は "粘土"を得ることはできません。

答えて

1

あなたがこれをどこでどのように柔軟にするのが好きですが、行を繰り返し処理し、その行のプロパティを検証しようとしています。代わりに、スクリプトはすべてのすべてのプロパティを反復処理しています行。ちょっと再構成しましょう。その後、

$data = .\hash_file.ps1 
$csv = import-csv .\ps_test.csv 

しかし、物事が横に行き始める:

あなたのハッシュテーブルやcsvファイルをロードすることにより、強力なアウトを開始、我々はそれをしておこう。今、それが輸入だ後、実際に[PSCustomObject]でその行のために、あなたはそのオブジェクトのあなたの特性を得ることができます(別名、

ForEach($Row in $CSV){ 

OK:何がしたいことは、各行を反復、それでは、そこに最初に行かせています列名)を参照してください。それではどうしよう:

$Row.PSObject.Properties.Name 

いいえ、これは私たちが扱っているすべてのプロパティを示しています。その後、我々はちょうどForEach-Objectループ内の各プロパティを見ることができます。

$Row.PSObject.Properties.Name | ForEach{ 
    "Column Name: $_" 
    "Row value for $_`: " + $Row.$_ 
    "Values to purge for $_`: " + $Data[$_] -join ', ' 
    If($Row.$_ -in $Data[$_]){ 
     "Record purged: " + $Row 
    } 
} 

は、それでは、そのすべて一緒に入れてみましょう:

私はあなたが欲しいかなり確信している何になり
$data = .\hash_file.ps1 
$csv = import-csv .\ps_test.csv 
ForEach($Row in $CSV){ 
    $Row.PSObject.Properties.Name | ForEach{ 
     "Column Name: $_" 
     "Row value for $_`: " + $Row.$_ 
     "Values to purge for $_`: " + $Data[$_] -join ', ' 
     If($Row.$_ -in $Data[$_]){ 
      "Record purged: " + $Row 
     } 
    } 
} 

Column Name: Name 
Row value for Name: clay 
Values to purge for Name: robert 
Column Name: Car 
Row value for Car: nissan exterra 
Values to purge for Car: nissan exterra 
Record purged: @{Name=clay; Car=nissan exterra} 
Column Name: Name 
Row value for Name: casey 
Values to purge for Name: robert 
Column Name: Car 
Row value for Car: honda accord 
Values to purge for Car: nissan exterra 
Column Name: Name 
Row value for Name: henry 
Values to purge for Name: robert 
Column Name: Car 
Row value for Car: nissan exterra 
Values to purge for Car: nissan exterra 
Record purged: @{Name=henry; Car=nissan exterra} 
Column Name: Name 
Row value for Name: robert 
Values to purge for Name: robert 
Record purged: @{Name=robert; Car=truck} 
Column Name: Car 
Row value for Car: truck 
Values to purge for Car: nissan exterra 
+0

うわー、ありがとうございました。私は上品な解説を好きだった。 :) –

+0

$ row.psobject.properties.nameがnullとして返されます。 –

+0

実行しているPowerShellのバージョンは何ですか?確かにv3以上のもので動作するはずです。 '$ Row'がnullでない限り。 – TheMadTechnician

0

この方法では、csvのデータを動的に操作できます。

[email protected]{ 
    name = [string[]]("robert"); 
    car = [string[]]("nissan exterra", "toyota") 
} 

import-csv "c:\temp\ps_test.csv" | %{ 
foreach ($key in $data.Keys) 
{ 
    foreach ($value in $data[$key]) 
    { 
     if ($_."$key" -EQ $value) 
     { 
      #you can purge here 
      #$_."$key"=$null 
     } 
    } 
} 

$_ 
} 
0

私はあなたがやろうとしているが、私はこれが役に立てば幸いものの一部を取得:

#step.ps1 -- steps through a csv file looking at each value 

$data = .\hash-file.ps1 
$csv = Import-Csv .\ps-test.csv 

foreach ($row in $csv) { 
    foreach ($column in $row.psobject.properties) { 
     "Here is the column name: " + $column.name 
     "Here is the current value: " + $column.value 
     "Here is the set of all values to purge: " + $data[$column.name] 
    } 
} 

は、ここで私は、ハッシュ・ファイルのために使用したものです:

#hash-file.ps1 

@{ 
    "name" = @("robert"); 
    "car" = @("nissan exterra") 
} 

そして、ここでは、私が使用したものですCSVファイル:

下線の代わりにハイフンを使用しています。 csvファイルにコメントを付けることができます。

私が言ったように、これは方法の一部に過ぎませんが、それが助けてくれることを願っています。

関連する問題