【问题标题】:UnicodeDecodeError with QFileDialog in PyQtPyQt 中带有 QFileDialog 的 UnicodeDecodeError
【发布时间】:2015-09-08 14:51:25
【问题描述】:

您好,当涉及到我拥有的文件对话框功能时,我的程序有问题。

首先是我的代码:

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)
  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 函数有关。我不确定

【问题讨论】:

    标签: python file pyqt filedialog


    【解决方案1】:

    那个特定的错误:

    UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)
    

    输入文件中的(意外)Byte Order Mark (BOM) 似乎存在问题。我怀疑日志文件是带有 BOM 的 UTF-8。

    尝试将文件打开行更改为:

    open(infile, 'rb', encoding='utf-8-sig')
    

    从文件中删除 BOM 标记。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-25
      • 2020-03-23
      • 2019-01-18
      • 2012-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多