尝试使用布尔比较进行文件搜索

时间:2016-06-14 23:04:47

标签: python-3.x glob

我试图编写Python脚本来查找目录中包含其文件内容中的两个关键字的文件。我之前发布了一个问题,提到了一个基本问题,这个代码的版本更简单,但我不确定是否需要单独发布,因为我现在正在查看另一个问题。

import glob
import os
import sqlite3
import re

conn = sqlite3.connect( "C:\\Users\\Jeff\\Documents\\GitHub\\YGOPro Salvation Server\\YGOPro-Support-System\\http\\ygopro\\databases\\0-en-OCGTCG.cdb" )
curs = conn.cursor()

#Define string constants
trig = "EFFECT_TYPE_TRIGGER"
summon = "SUMMON_SUCCESS"
flip = "EFFECT_TYPE_FLIP"
flip2 = "EVENT_FLIP"
pos = "EVENT_CHANGE_POS"
spelltrap = "EFFECT_TYPE_ACTIVATE"
banish = "EVENT_REMOVE"
grave = "EVENT_TO_GRAVE"

os.chdir( "C:\\Users\\Jeff\\Documents\\GitHub\\YGOPro Salvation Server\\Salvation-Scripts-TCG" )
for files in glob.glob( "*.lua" ) :
    f = open( files, 'r', encoding = "iso-8859-1" )
    for line in f :
        files = re.sub('[c.luatilityold]', '', files)
        #Use database to print names corresponding to each file ID for verification purpose
        result = curs.execute("SELECT id, name FROM texts WHERE ID=?", (files,))
        x = result.fetchone()
        #Check for files that have both 'trig' and 'banish' values in contents
        if trig and banish in line :
            if x is not None :
                print ( x )
        #Check for files that have both 'trig' and 'grave' values in contents
        elif  trig and grave in line :
            if x is not None :
                print ( x )
        #Check for files that have both 'trig' and 'summon' values in contents
        elif trig and summon in line :
            if x is not None :
                print ( x )
        #Check for files that have 'flip' value in contents
        elif flip in line :
            if x is not None :
                print ( x )
        #Check for files that have both 'trig' and 'flip2' values in contents
        elif trig and flip2 in line :
            if x is not None :
                print ( x )
        #Check for files that have both 'trig' and 'pos' values in contents
        elif trig and pos in line :
            if x is not None :
                print ( x )
        #Ignore other files
        else :
            pass

我遇到的问题是if-cases无法正常工作。 trig变量在代码运行时被忽略,因此它只查看第二个键。我曾尝试使用诸如if trig in line and banish in line之类的措辞,但问题是它只会在文件内容的同一行上查找具有这两个键的文件。我需要这个能够找到文件中任意位置有两个键的文件。是否有更好的方法一次性搜索两个键,就像我试图做的那样,或者我需要采取不同的方法?

1 个答案:

答案 0 :(得分:0)

由于此代码依赖于特定于您的计算机的数据库,因此无法测试我的更改是否可以创建预期的输出。

我不确定你在这里想做什么。如果要在整个文件中搜索关键字,请立即读取整个文件,而不是逐行读取。

import glob
import os
import sqlite3
import re

conn = sqlite3.connect( "C:\\Users\\Jeff\\Documents\\GitHub\\YGOPro Salvation Server\\YGOPro-Support-System\\http\\ygopro\\databases\\0-en-OCGTCG.cdb" )
curs = conn.cursor()

#Define string constants
trig = "EFFECT_TYPE_TRIGGER"
summon = "SUMMON_SUCCESS"
flip = "EFFECT_TYPE_FLIP"
flip2 = "EVENT_FLIP"
pos = "EVENT_CHANGE_POS"
spelltrap = "EFFECT_TYPE_ACTIVATE"
banish = "EVENT_REMOVE"
grave = "EVENT_TO_GRAVE"

os.chdir( "C:\\Users\\Jeff\\Documents\\GitHub\\YGOPro Salvation Server\\Salvation-Scripts-TCG" )
for filename in glob.glob( "*.lua" ) :
    with open(filename, 'r', encoding = "iso-8859-1") as content_file:
        content = content_file.read()
        query_file = re.sub('[c.luatilityold]', '', filename)
        #Use database to print names corresponding to each file ID for verification purpose
        result = curs.execute("SELECT id, name FROM texts WHERE ID=?", (query_file ,))
        x = result.fetchone()
        #Check for files that have both 'trig' and 'banish' values in contents
        if trig in content and banish in content:
            if x is not None :
                print ( x )
        #Check for files that have both 'trig' and 'grave' values in contents
        elif  trig in content and grave in content:
            if x is not None :
                print ( x )
        ...

您肯定想使用语法

if x in content and y in content

否则,条件将始终计算为True,因为该字符串始终为非空,因此始终为True(如上一篇文章中的注释所示)。

我在您的代码中假设在

中重用变量files
files = re.sub('[c.luatilityold]', '', files)
    #Use database to print names corresponding to each file ID for verification purpose
    result = curs.execute("SELECT id, name FROM texts WHERE ID=?", (files,))
    x = result.fetchone()

是偶然的。更改files中存储的值不应影响此实例中的任何其他内容,但如果您尝试获取当前文件的文件路径,则无法获得预期的结果。因此,我还建议像我一样使用不同的变量进行此操作。

相关问题