[英]UnicodeDecodeError with QFileDialog in PyQt
您好,當我遇到文件對話框功能時,我的程序遇到問題。
首先這里是我的代碼:
def getFileInfo(self):
global logName
logName = QtGui.QFileDialog.getOpenFileName()
return logName
def getFileName(self):
return logName
def compareAction(self):
def process(infile, outfile, keywords):
keys = [[k[0], k[1], 0] for k in keywords]
endk = None
with open(infile, 'rb') as fdin:
with open(outfile, 'ab') as fdout:
fdout.write("<" + words + ">" + "\r\n")
for line in fdin:
if endk is not None:
fdout.write(line)
if line.find(endk) >= 0:
fdout.write("\r\n")
endk = None
else:
for k in keys:
index = line.find(k[0])
if index >= 0:
fdout.write(line[index + len(k[0]):].lstrip())
endk = k[1]
k[2] += 1
if endk is not None:
raise Exception(endk + "Not found before end of file")
return keys
clearOutput = open('test.txt', 'wb')
clearOutput.truncate()
clearOutput.close()
outputText = 'test.txt'
end_token = "[+][+]"
inputFile = logName
start_token = self.serialInputText.toPlainText()
split_start = start_token.split(' ')
for words in split_start:
process(inputFile,outputText,((words + "SHOWALL"),))
fo = open(outputText, "rb")
text = fo.read()
print start_token + '\r\n'
print split_start
print inputFile
好的,所以這段代碼的總體思路是從我的PyQt GUI中的TextEdit抓取一些輸入的文本。 然后,將該字符串分成一個列表,該列表可用於“ 掃描 ”整個文件,如果有匹配項,則將這些匹配項打印到另一個文本文檔中。
腳步:
This is a list
-> [u'This', u'Is', u'A', u'List']
(由於我使用sip
編寫了代碼 [u'This', u'Is', u'A', u'List']
該列表具有au) def process
函數了。 def getFileInfo(self)
和def GetFileName(Self)
函數起作用的地方。 def compareAction(self)
函數。 問題
當前,我的問題是執行所有步驟后在步驟6上失敗后出現此錯誤。這是我的錯誤:
Traceback (most recent call last):
File "RETRACTED.py", line 278, in compareAction
process(inputFile,outputText,((words + "SHOWALL"),))
File "RETRACTED.py", line 260, in process
index = line.find(k[0])
UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)
我不確定為什么會發生此錯誤。 我一直在尋找類似的問題,但我認為這與我的process
功能有關。 我不確定
該特定錯誤:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)
看起來像輸入文件中(意外) 字節順序標記 (BOM)的問題。 我懷疑日志文件是帶有BOM的UTF-8。
嘗試將文件打開行更改為:
open(infile, 'rb', encoding='utf-8-sig')
將BOM表標記從文件中刪除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.