我有一个python类,允许用户选择路径以显示文件,然后用户输入搜索的单词以便将其与所有列出的文件相匹配。
一旦用户点击搜索按钮,我需要系统
系统显示此错误:
searchWord self.listWidgetPDFlist.addItems(listFiles)
builtins.TypeError:index 0的类型为' bool'但是' str'预计
为此任务编写了3个函数
def __init__(self,PdfPreviewObj):
#QWidget.__init__(self)
self.PdfPreviewObj =PdfPreviewObj
self.setupUi(PdfPreviewObj)
self.PdfPreviewObj.show()
self.pushButtonOpenFolder.clicked.connect(self.setExistingDirectory)
self.pushButtonSearch.clicked.connect(self.searchWord)
def readFile(self, currentFile):
try:
currentFile = self.listWidgetPDFlist.currentItem().text()
print(currentFile)
with open(currentFile) as ctf:
ctfRead = ctf.read()
print(ctfRead)
return(ctfRead)
except Exception as e:
print("the selected file is not readble because : {0}".format(e))
def displayFilteredFiles(self, filteredFiles):
try:
filteredFiles = self.listWidgetPDFlist.items()
print(filteredFiles)
for file in filteredFiles:
with open(file) as ctf:
ctfRead = ctf.read()
print(ctfRead)
return(ctfRead)
except Exception as e:
print("the selected file is not readble because : {0}".format(e))
def searchWord(self,filteredFiles):
listFiles = []
self.textEdit_PDFpreview.clear()
self.listWidgetPDFlist.clear()
listFiles.append(filteredFiles)
# here the system indicate the error
self.listWidgetPDFlist.addItems(listFiles)
filesToSearchInside = self.displayFilteredFiles(listFiles)
searchedSTR = self.lineEditSearch.text()
RepX="<b><span style='color:white;mso-themecolor:background1;background:black;mso-highlight:black'>"+searchedSTR+'</span></b>'
try:
textList = filesToSearchInside.split('\n')
# utility that gives an empty list for each key by default
d = defaultdict(list)
'''
looping over the selected text and search for the required word in the search box
store the word and the number of occurence in a dictionnary to used for later.
'''
for counter, myLine in enumerate(textList):
thematch=re.sub(searchedSTR,RepX,myLine)
matches = re.findall(searchedSTR, myLine, re.MULTILINE | re.IGNORECASE)
if len(matches) > 0:
# add one record for the match (add one because line numbers start with 1)
d[matches[0]].append(counter + 1)
self.textEdit_PDFpreview.insertHtml(str(thematch))
#self.textEdit_PDFpreview.insertHtml(result)
# print out
for match, positions in d.items():
print('{} exists {} times'.format(match, len(positions)))
for p in positions:
print("on line {}".format(p))
except Exception as e:
print(e)