通过共享列值过滤和组合CSV文件,然后输出到新的CSV

发布于 2025-01-22 04:19:28 字数 1665 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左秋 2025-01-29 04:19:28

您可以使用 group-object-ashtable 生成actactiondswithzips.csv.csv的哈希表在搜索匹配 zips 时,有助于快速查找:

$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

You can use Group-Object -AsHashTable to generate a hash table of the AttractionIDsWithZips.csv, this helps allows for fast lookup when searching for matching Zips:

$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

The result I got using the example CSVs provided in the question looks like this:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文