通过共享列值过滤和合并 CSV 文件并输出到新的 CSV
Filtering and Combining CSV Files via Shared Column Values and Outputting to a New CSV
PowerShell 新手,但开始掌握一些基础知识!
感谢大家的耐心等待:)
我正在尝试将一个文件中的某些邮政编码数据与第二个文件中的匹配邮政编码和景点 ID 相匹配。
文件 1 是我的 'master' = "ZipResults.csv" = 与其他每个邮政编码相距 50 英里以内的邮政编码列表。有 3 列,但没有 headers。但是,它们的组织方式如下:
示例细分:
SearchedZip FoundZip Radius
----------- -------- ------
12345 12345 50
12345 12346 50
12345 12347 50
12346 12346 50
12346 12344 50
12346 12347 50
12347 12347 50
12347 12346 50
12347 12349 50
第二个文件 =“AttractionIDsWithZips.csv”,它有 headers“ID,Zip”,类似于:
ID ZIP
-- ---
112484 12346
112486 12346
5548 12347
112491 12345
5583 12349
112480 12344
我想使用第二个文件的“Zip”列中的值从第一个文件中查找匹配的“SearchedZip”行,然后将 AttractionID 与每个匹配的“FoundZips”值配对第一个文件,然后输出到第三个文件...效果如下:
AttractionId MatchedZip Radius
------------ ---------- ------
112484 12346 50
112484 12347 50
112484 12348 50
112486 12346 50
112486 12348 50
112486 12344 50
5548 12347 50
5548 12348 50
5548 12349 50
112491 12345 50
112491 12346 50
112491 12344 50
我当前的代码是这样的,但它没有得到任何输出...似乎在一个没有得到任何结果的 super-long 循环中:
$hauntIdStuff = Import-Csv -Path .\AttractionIDsWithZips.csv | Select-Object -ExpandProperty Zip<br>
Import-Csv -Path .\Zips1kTo2k.csv -Header "zipS","zipF","zipR" | Where-Object {$hauntIdStuff.Zip -contains $_.zipS} | ForEach-Object {<br>
Add-Content -Path .\IDsWithMatchingZips.csv -Value "$($_.ID),$($_.zipF),$($_.zipR)"<br>
}
您可以使用 Group-Object -AsHashTable
生成 AttractionIDsWithZips.csv
的散列 table,这有助于在搜索匹配 Zips[=20= 时进行快速查找]:
$refTable = Import-Csv Zips1kTo2k.csv -Header 'SearchedZip', 'FoundZip', 'Radius' |
Group-Object SearchedZip -AsHashTable -AsString
& {
foreach($line in Import-Csv AttractionIDsWithZips.csv) {
if($values = $refTable[$line.zip]) {
foreach($value in $values) {
[pscustomobject]@{
AttractionId = $line.ID
MatchedZip = $value.SearchedZip
Radius = $value.Radius
}
}
}
}
} | Export-Csv IDsWithMatchingZips.csv -NoTypeInformation
我使用问题中提供的示例 CSV 得到的结果如下所示:
AttractionId MatchedZip Radius
------------ ---------- ------
112484 12346 50
112484 12346 50
112484 12346 50
112486 12346 50
112486 12346 50
112486 12346 50
5548 12347 50
5548 12347 50
5548 12347 50
112491 12345 50
112491 12345 50
112491 12345 50
PowerShell 新手,但开始掌握一些基础知识!
感谢大家的耐心等待:)
我正在尝试将一个文件中的某些邮政编码数据与第二个文件中的匹配邮政编码和景点 ID 相匹配。
文件 1 是我的 'master' = "ZipResults.csv" = 与其他每个邮政编码相距 50 英里以内的邮政编码列表。有 3 列,但没有 headers。但是,它们的组织方式如下:
示例细分:
SearchedZip FoundZip Radius
----------- -------- ------
12345 12345 50
12345 12346 50
12345 12347 50
12346 12346 50
12346 12344 50
12346 12347 50
12347 12347 50
12347 12346 50
12347 12349 50
第二个文件 =“AttractionIDsWithZips.csv”,它有 headers“ID,Zip”,类似于:
ID ZIP
-- ---
112484 12346
112486 12346
5548 12347
112491 12345
5583 12349
112480 12344
我想使用第二个文件的“Zip”列中的值从第一个文件中查找匹配的“SearchedZip”行,然后将 AttractionID 与每个匹配的“FoundZips”值配对第一个文件,然后输出到第三个文件...效果如下:
AttractionId MatchedZip Radius
------------ ---------- ------
112484 12346 50
112484 12347 50
112484 12348 50
112486 12346 50
112486 12348 50
112486 12344 50
5548 12347 50
5548 12348 50
5548 12349 50
112491 12345 50
112491 12346 50
112491 12344 50
我当前的代码是这样的,但它没有得到任何输出...似乎在一个没有得到任何结果的 super-long 循环中:
$hauntIdStuff = Import-Csv -Path .\AttractionIDsWithZips.csv | Select-Object -ExpandProperty Zip<br>
Import-Csv -Path .\Zips1kTo2k.csv -Header "zipS","zipF","zipR" | Where-Object {$hauntIdStuff.Zip -contains $_.zipS} | ForEach-Object {<br>
Add-Content -Path .\IDsWithMatchingZips.csv -Value "$($_.ID),$($_.zipF),$($_.zipR)"<br>
}
您可以使用 Group-Object -AsHashTable
生成 AttractionIDsWithZips.csv
的散列 table,这有助于在搜索匹配 Zips[=20= 时进行快速查找]:
$refTable = Import-Csv Zips1kTo2k.csv -Header 'SearchedZip', 'FoundZip', 'Radius' |
Group-Object SearchedZip -AsHashTable -AsString
& {
foreach($line in Import-Csv AttractionIDsWithZips.csv) {
if($values = $refTable[$line.zip]) {
foreach($value in $values) {
[pscustomobject]@{
AttractionId = $line.ID
MatchedZip = $value.SearchedZip
Radius = $value.Radius
}
}
}
}
} | Export-Csv IDsWithMatchingZips.csv -NoTypeInformation
我使用问题中提供的示例 CSV 得到的结果如下所示:
AttractionId MatchedZip Radius
------------ ---------- ------
112484 12346 50
112484 12346 50
112484 12346 50
112486 12346 50
112486 12346 50
112486 12346 50
5548 12347 50
5548 12347 50
5548 12347 50
112491 12345 50
112491 12345 50
112491 12345 50