我有一个程序,允许用户选择文件路径以搜索关键字。
当用户输入一个单词时,该程序工作正常,但当他输入的单词超过1个单词时,它不会显示任何内容。
我的函数中的错误在哪里?
def HighLight(self,x,TextString,RepX):
thematch=TextString.replace(x,RepX)
TheCount=TextString.count(x)
self.textEdit_PDFpreview.insertHtml(str(thematch))
return thematch
def MatchFunc(self): # Function to highlight the found criteria and display it in the right pane
self.textEdit_PDFpreview.clear()
self.x = self.lineEditSearch.text().strip()
TextString=self.ReadingFileContent(self.FileListSelected())
self.z = ""
counter = 0
self.d = defaultdict(list)
self.filename = os.path.basename(self.FileListSelected())
for counter , myLine in enumerate(self.filename):
y=list(self.x.split())
print(y)
for self.z in y:
RepX='<u><b style="color:#FF0000">'+self.z+'</b></u>'
self.textEdit_PDFpreview.clear()
if self.z in TextString:
self.d[self.z].append(counter + 1)
TextString = self.HighLight(self.z,TextString,RepX)
elif self.x in TextString:
RepX='<u><b style="color:#FF0000">'+self.x+'</b></u>'
self.textEdit_PDFpreview.clear()
self.d[self.x].append(counter + 1)
self.HighLight(self.x,TextString,RepX)
答案 0 :(得分:0)
您的代码中存在多个问题。
SELECT * FROM object_action_statistics WHERE day IN ('2018-04-29','2018-04-30') AND action_id=14 AND timestamp_from>=1525099500073 AND timestamp_from<1525120897000 ALLOW FILTERING
不需要。只需拆分您的输入,如果没有黑色空格,它将返回if " " in self.x:
一个list
element
传递给sep=" "
,这是默认行为split
循环。首先if-else
,split
然后self.x
循环for
。至于为什么它不适用于多输入,可能是由于:
y
在TextString = self.HighLight(self.z,TextString,RepX)
中,您不会存储此else
值,但只能肯定地说明,如果您向我们展示了return
的作用。
<强>更新强>
您正在self.HighLight
圈中查看self.z in TextString:
,而在if
中,您的支票为else
。 self.x in TextString
是self.z
字符串。这是故意的吗?为什么不同时检查这两种情况呢?
根据您的问题,当用户提供多个输入时,您希望代码具有相同的行为,在这种情况下,两种情况都应该具有相同的逻辑。
因此,我的第一个建议是删除重复的代码。