不正な値の行を削除するプログラムを作成しています。列名はあらかじめわかっていないので、ハードコーディングされていないことが重要です。私は、データ構造のインデックスとして列ヘッダーを使用して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"。しかし、私は "粘土"を得ることはできません。
うわー、ありがとうございました。私は上品な解説を好きだった。 :) –
$ row.psobject.properties.nameがnullとして返されます。 –
実行しているPowerShellのバージョンは何ですか?確かにv3以上のもので動作するはずです。 '$ Row'がnullでない限り。 – TheMadTechnician