簡體   English   中英

PyQt中帶有QFileDialog的UnicodeDecodeError

[英]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抓取一些輸入的文本。 然后,將該字符串分成一個列表,該列表可用於“ 掃描 ”整個文件,如果有匹配項,則將這些匹配項打印到另一個文本文檔中。

腳步:

  1. 用戶在TextEdit中輸入文本
  2. TextEdit中的文本存儲到QString中
  3. 該QString有一個空格作為定界符,因此我們將每個條目拆分為一個列表。 即, This is a list -> [u'This', u'Is', u'A', u'List'] (由於我使用sip編寫了代碼 [u'This', u'Is', u'A', u'List'] 該列表具有au)
  4. 現在我們有了這個QStringList,我們可以將其傳遞給我的def process函數了。
  5. 我們顯然需要一個文件來搜索,這就是def getFileInfo(self)def GetFileName(Self)函數起作用的地方。
  6. 因此,在用戶輸入了一些文本,選擇了要搜索的文件之后,他/她將按下一個按鈕,將其稱為CompareButton,然后它將執行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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM