通过共享列值过滤和组合CSV文件,然后输出到新的CSV
Powershell的新手,但开始掌握一些基本知识!
感谢每个人的耐心:)
我正在尝试将一个文件中的一些邮政编码数据与第二个文件中匹配的ZIP代码和景点ID匹配。
文件1是我的'master'=“ zipresults.csv” =邮政编码列表,该邮政编码位于其他邮政编码50英里以内。有3列,但没有标题。但是,它们的组织如下:
示例段:
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
第二个file =“ factionIdSwithzips.csv”,它的标题为“ id,zip”,看起来像:
ID ZIP
-- ---
112484 12346
112486 12346
5548 12347
112491 12345
5583 12349
112480 12344
我想使用来自该列的“ zip”列中的值第二个文件可以从第一个文件中查找匹配的“ searchedzip”行,然后将actactionId与第一个文件中的每个匹配的“ bentzips”值配对,然后输出到第3个文件...效果:
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
我当前代码 的效果:是这样的,但是没有得到任何输出...似乎是一个没有得到任何结果的超长循环:
$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>
}
New to PowerShell, but starting to get the hang of some basics!
Appreciate everyone's patience :)
I'm trying to match some ZIP code data from one file with matching ZIP codes and Attraction ID's in a second file.
File 1 is my 'master' = "ZipResults.csv" = a list of ZIP codes that are within 50 miles of every other ZIP code. There are 3 columns, but no headers. However, they are organized as follows:
Example segment:
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
The second file = "AttractionIDsWithZips.csv", which has headers "ID,Zip" and looks something like:
ID ZIP
-- ---
112484 12346
112486 12346
5548 12347
112491 12345
5583 12349
112480 12344
I'd like to use the values from the "Zip" column of the second file to find matching "SearchedZip" rows from the first file, then pair the AttractionID's with each of their matching "FoundZips" values from the first file, and output to a 3rd file... something to the effect of:
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
My current code is like this, but it's not getting any output... seems to be in a super-long loop that's not getting any results:
$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>
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
group-object-ashtable
生成actactiondswithzips.csv.csv
的哈希表在搜索匹配 zips 时,有助于快速查找:我使用问题中提供的示例CSV的结果如下:
You can use
Group-Object -AsHashTable
to generate a hash table of theAttractionIDsWithZips.csv
, this helps allows for fast lookup when searching for matching Zips:The result I got using the example CSVs provided in the question looks like this: