用于查找特定参数的Python代码

2024-10-02 10:23:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我做了一段时间的项目。这是与ACL的工作,不幸的是,野兽的性质是披露是不可能的,所以我将提供尽可能多的细节,我可以不透露任何东西。非常感谢您的帮助!你知道吗

我一直坚持要自己找到解决方案,但恐怕我对Python的了解不够,无法达到预期的效果。我已经在批处理、PowerShell甚至Bash中实现了这个结果。。。它只是没有在网络路径上以快速的速度工作,不像我们的其他Python脚本那样先在内存中缓冲,然后快速而平滑地工作。请注意,这将筛选超过500K-5M行的数据。你知道吗

示例数据.csv

GROUPS,PATH,EXPLICIT,STUFF
Group1,folder0,Explicit,somestuffhere
Group2,folder0,Explicit,somestuffhere
Group1,folder0\coolbeans,Implicit,somestuffhere
Group2,folder0\coolbeans,Implicit,Somestuffhere
Group3,folder0\coolbeans\awesomesauce,Implicit,Somestuffhere
Group3,folder1,Explicit,Somestuffhere
Group4,folder1\awesome,Implicit,Somestuffhere
Group5,folder1\awesome,Implicit,Somestuffhere

概念:

  • 生成显式存在的每个组的列表。

  • 生成隐式存在的每个组的列表。

  • 两个列表都是唯一的(删除重复项)

  • 比较两个列表,对于隐式组和显式组。

  • 按其余组筛选整个文件内容:

  • 将每一行以及剩余的组整行输出到单个文件中。

理论:

  • 正在查找Microsoft的移动到网络(拖放)文件夹继承问题。你知道吗

我知道这不仅会帮助我自己,也会帮助许多其他人寻求这个挑战。你知道吗

谢谢你的帮助!你知道吗

附加说明:Python版本2.6.6

根据管理员的要求,我以前使用过一个代码示例:如前所述。你知道吗

    import csv
    import sys
    global exp
    global imp
    exp = set()
    imp = set()
    exc = set()
    i = open(sys.argv[1], "rb")
    csvr = csv.reader(i)

    def expparse(target):
        if "Explicit" in target:
            exp.add(target[0])

    def impparse(target):
        if "Implicit" in target:
            imp.add(target[0])


    if __name__ == "__main__":
        for rows in csvr:
            expparse(rows)
            impparse(rows)

    exc = imp - exp # added with aid of Joran - Lack of knowledge to compare 2 Sets.
    for rows in (exc):
        print rows # Produces the right information, how do I set it as a filter?

到目前为止,我仍然不确定现在如何比较整个CSV的异常。我的目的是看看其他人会产生什么类型的代码。我想看看其他拥有更多python经验的人有什么想法,如果不带偏见的话这是有意义的话。你知道吗

反过来,如何让python获取与exc集中的行匹配的每一行?你知道吗

谢谢!你知道吗


Tags: csvintarget列表rowsexcimpset
2条回答
import csv
from itertools import izip, tee

INPUT = "some.csv"
OUTPUT = "filtered.csv"

implicit_groups = dict()
explicit_groups = set()

with open(INPUT, "rb") as inf:
    lines, to_rows = tee(inf)
    rows = csv.reader(to_rows)
    for line, row in izip(lines, rows):
        group, _, state, _ = row
        if state == "Explicit":
            implicit_groups.pop(group, None)
            explicit_groups.add(group)
        elif state == "Implicit":
            if group not in explicit_groups:
                implicit_groups[group] = line

with open(OUTPUT, "wb") as outf:
    # edit!  writing lines, not rows:
    outf.write("\n".join(implicit_groups.itervalues()))
groups_explicit,groups_implicit = set(),set)_
with open(some_csv) as f:
    for line in f:
        if "Explicit" in line:
           groups_explicit.add(line.split(",",1)[0]) 
        elif "Implicit" in line:
           else groups_implicit.add(line.split(",",1)[0])
# use set difference to tell which groups are in implicit but not explicit
implicit_groups_not_in_explicit = groups_implicit - groups_explicit

可能就是你要找的。。。很难说

相关问题 更多 >

    热门问题