1. 程式人生 > >fool.load_userdict(path)出現編碼檔案出錯,UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position

fool.load_userdict(path)出現編碼檔案出錯,UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position

錯誤問題:UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 34: illegal multibyte sequence

解決方法:    

      解決辦法1

             FILE_OBJECT= open('order.log','r', encoding='UTF-8')

      解決辦法2.

             FILE_OBJECT= open('order.log','rb')

          以上都無法解決!

GitHub上的原始碼:with open(path,'r',encoding='UTF-8') as f:

    def add_dict(self, path):
        words = []

        with open(path,'r',encoding='UTF-8') as f:
            for i, line in enumerate(f):
                line = line.strip("\n").strip()
                if not line:
                    continue
                line = line.split()
                word = line[0].strip()
                self.trie.add_keyword(word)
                if len(line) == 1:
                    weight = 1.0
                else:
                    weight = float(line[1])
                weight = float(weight)
                self.weights[word] = weight
                words.append(word)
        self.sizes += len(self.weights)

python上安裝的包裡程式碼為: with open(path) as f:

    def add_dict(self, path):
        words = []

        with open(path) as f:
            for i, line in enumerate(f):
                line = line.strip("\n").strip()
                if not line:
                    continue
                line = line.split()
                word = line[0].strip()
                self.trie.add_keyword(word)
                if len(line) == 1:
                    weight = 1.0
                else:
                    weight = float(line[1])
                weight = float(weight)
                self.weights[word] = weight
                words.append(word)
        self.sizes += len(self.weights)

最終解決辦法:將包內程式碼改為GitHub上的程式碼問題完美解決!