在python中如何计算两个文件之间的匹配?

2024-05-23 18:41:04 发布

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

我是新使用python的,问题是我不知道从哪里开始可以解决下面的问题

我有三个文件

1文件包含如下内容:

pm_rpm@fences
pm_rpm@fences-dpms
pm_rpm@modeset-lpsp-stress
pm_rpm@modeset-non-lpsp-stress
pm_rpm@modeset-lpsp-stress-no-wait
pm_rpm@modeset-non-lpsp-stress-no-wait
pm_rpm@modeset-pc8-residency-stress
pm_rpm@modeset-stress-extra-wait
pm_rpm@system-suspend
pm_rpm@system-suspend-execbuf
pm_rpm@system-suspend-modeset

这个1文件包含我将运行的所有测试

第二个文件如下所示:

kms_vblank@accuracy
kms_vblank@query-idle
kms_vblank@query-busy
kms_vblank

第三个文件如下所示:

kms_flip@2x-rcs-wf_vblank-vs-modeset
kms_flip@basic-plain-flip
kms_flip@2x-plain-flip
kms_flip@busy-flip
kms_flip@2x-busy-flip

所以基本上我需要检查文件2和文件3中有多少测试在文件1中,并得到如下输出

file 2 : 3 tests found
file 3 : 10 tests found

但例如,如果在文件2或文件3中的任何一个文件中,我有一个通配符测试,如下所示:

gem\U并发*

我需要python脚本与通配符一起工作

非常感谢你在这方面的帮助


Tags: 文件systemrpmnonwaitsuspendkmsstress
2条回答

我用Python3编写了这些程序,第一个只包括'@'符号后面的测试,第二个包括整个测试名称:

file1=open("file1.txt")
file2=open("file2.txt")
file3=open("file3.txt")
f1=file1.read().split("\n")
f2=file2.read().split("\n")
f3=file3.read().split("\n")
file1.close()
file2.close()
file3.close()

f1s,f2s,f3s=[],[],[]        #file1 split, file2 split, file3 split
try: [f1s.append(i.split("@")[1]) for i in f1]
except IndexError: pass

try: [f2s.append(i.split("@")[1]) for i in f2]
except IndexError: pass

try: [f3s.append(i.split("@")[1]) for i in f3]
except IndexError: pass

match2=0
for i in f2s:
    if i[-1:]=="*":
        i=i[:-1]
        if [e for e in f1s if e[:len(i)]==i]: match2+=1
    elif i in f1s: match2+=1

match3=0
for i in f3s:
    if i[-1:]=="*":
        i=i[:-1]
        if [e for e in f1s if e[:len(i)]==i]: match3+=1
    elif i in f1s: match3+=1

print("file 2:", match2, "tests found")
print("file 3:", match3, "tests found")

第二个程序:

f1=open("file1.txt")
f2=open("file2.txt")
f3=open("file3.txt")
f1s=f1.read().split("\n")
f2s=f2.read().split("\n")
f3s=f3.read().split("\n")
f1.close()
f2.close()
f3.close()

match2=0
for i in f2s:
    if i[-1:]=="*":
        i=i[:-1]
        if [e for e in f1s if e[:len(i)]==i]: match2+=1
    elif i in f1s:
        match2+=1
match3=0
for i in f3s:
    if i[-1:]=="*":
        i=i[:-1]
        if [e for e in f1s if e[:len(i)]==i]: match3+=1
    elif i in f1s:
        match3+=1
print("file 2:", match2)
print("file 3:", match3)

通过我所做的测试,它们似乎都可以工作,并且也支持通配符,但是如果其中任何一个有问题,请给出注释。你知道吗

希望这有帮助。你知道吗

使用python2.7有一个非常愚蠢的解决方案:

import re

def findCommon(a,b):
    allmatches=[]
    for test in b:
        if "*" in test or "?" in test:
            reg=re.compile(test)
            matches=[line for line in a for m in [reg.search(line)] if m]
        else:
            matches=[line for line in a if test==line]
        allmatches.extend(matches)
    c=set(allmatches)
    return len(c)

def main():
    content1=[]
    content2=[]
    content3=[]
    with open('file1.txt','r') as f1:
        content1=[line.strip() for line in f1]
    with open('file2.txt','r') as f2:
        content2=[line.strip() for line in f2]
    with open('file3.txt','r') as f3:
        content3=[line.strip() for line in f3]

    print "file 2 :", findCommon(content1,content2),"tests found"
    print "file 3 :", findCommon(content1,content3),"tests found"

main()

它首先将整个文件读入三个列表,然后在两个列表对上调用findCommon函数。你知道吗

findCommon函数遍历第二个列表(文件)并检查测试名是否包含通配符(当然这假设只有两个通配符?和*,这可能是错误的)。如果是这样,它将在第一个文件上使用regex匹配,否则将使用简单的等式来查找匹配项。 set用于删除重复的匹配项。你知道吗

相关问题 更多 >