我正在编写一个程序来删除带有错误值的行。列名称事先是未知的,因此重要的是它们不是硬编码的。我正在尝试使用列标题作为数据结构的索引来读取CSV文件。我使用MemberType
属性存储CSVs属性。这让我得到了名字而不是价值!
这是我的代码:
$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
}
}
}
以下是我的结果:
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
要清除的项目存储为哈希表中的列,值对:
#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”。但我不能得到“粘土”。
答案 0 :(得分:1)
好吧,我有点喜欢你正在使用它以及它有多灵活,但你试图遍历行,并验证该行的属性,而你的脚本正在迭代所有属性所有行。让我们进行一些重组。
你通过加载哈希表和csv开始强大,我们会保留:
$data = .\hash_file.ps1
$csv = import-csv .\ps_test.csv
然而事情开始横行了。你想要做的是迭代每一行,所以让我们先去那里:
ForEach($Row in $CSV){
好的,现在对于该行,一旦导入它实际上是[PSCustomObject]
,您可以通过引用PSObject隐藏属性来获取该对象的属性(也就是列名)。所以,让我们这样做:
$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
答案 1 :(得分:0)
您可以使用其他方法动态处理csv的数据:
$data=@{
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
}
}
}
$_
}
答案 2 :(得分: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文件的内容:
#ps-test.csv
name,car
clay,nissan exterra
casey,honda accord
henry,nissan exterra
robert, truck
请注意,我使用了连字符而不是下划线。 请注意,csv文件中可以包含注释。
正如我所说,这只是其中的一部分,但我希望它有所帮助。