尝试使用布尔比较进行文件搜索
Trying to use boolean comparison for file search
我正在尝试编写一个 Python 脚本来查找文件内容中包含两个关键字的目录中的文件。之前我已经 post 提出了一个问题,该问题涉及一个基本问题,该问题的版本更简单,但我不确定是否需要单独 post 这个问题,因为我现在正在查看不同的问题。
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
等措辞,但问题是它只会查找在文件内容的同一行上同时具有这两个键的文件。我需要这个才能找到文件中任何位置都有两个键的文件。是否有更好的方法可以像我尝试的那样一次搜索这两个密钥,或者我需要采用其他方法吗?
由于此代码依赖于特定于您的计算机的数据库,因此我无法测试我的更改是否可以创建您预期的输出。
我不太确定你想在这里做什么。如果要在整个文件中搜索关键字,请一次性读入整个文件,而不是逐行读入。
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(如您上次 post 中的评论所示)。
我假设在你的代码中重用 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
中的值不应影响此实例中的任何其他内容,但如果您尝试获取当前文件的文件路径,您将无法获得预期的结果。因此,我也建议像我一样为此操作使用不同的变量。
我正在尝试编写一个 Python 脚本来查找文件内容中包含两个关键字的目录中的文件。之前我已经 post 提出了一个问题,该问题涉及一个基本问题,该问题的版本更简单,但我不确定是否需要单独 post 这个问题,因为我现在正在查看不同的问题。
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
等措辞,但问题是它只会查找在文件内容的同一行上同时具有这两个键的文件。我需要这个才能找到文件中任何位置都有两个键的文件。是否有更好的方法可以像我尝试的那样一次搜索这两个密钥,或者我需要采用其他方法吗?
由于此代码依赖于特定于您的计算机的数据库,因此我无法测试我的更改是否可以创建您预期的输出。
我不太确定你想在这里做什么。如果要在整个文件中搜索关键字,请一次性读入整个文件,而不是逐行读入。
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(如您上次 post 中的评论所示)。
我假设在你的代码中重用 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
中的值不应影响此实例中的任何其他内容,但如果您尝试获取当前文件的文件路径,您将无法获得预期的结果。因此,我也建议像我一样为此操作使用不同的变量。