UnicodeDecodeError:' utf8'编解码器不能解码位置377826中的字节0x92:无效的起始字节

时间:2015-04-02 17:51:24

标签: git github encoding utf-8

我在行if uID in repo.git.log():处执行以下代码段时收到以下错误, 问题出在repo.git.log(),我查看了Stack Overflow上所有类似的问题,建议使用decode("utf-8")

如何将repo.git.log()转换为decode("utf-8")

UnicodeDecodeError: 'utf8' codec can't decode byte 0x92 in position 377826: invalid start byte 

相关代码:

..................
uID = gerritInfo['id'].decode("utf-8")                                            
if uID in repo.git.log():
        inwslist.append(gerritpatch)      
.....................


Traceback (most recent call last):
  File "/prj/host_script/script.py", line 1417, in <module>
    result=main()
  File "/prj/host_script/script.py", line 1028, in main
    if uID in repo.git.log():
  File "/usr/local/lib/python2.7/dist-packages/git/cmd.py", line 431, in <lambda>
    return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/git/cmd.py", line 802, in _call_process
    return self.execute(make_call(), **_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/git/cmd.py", line 610, in execute
    stdout_value = stdout_value.decode(defenc)
  File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0x92 in position 377826: invalid start byte

2 个答案:

答案 0 :(得分:10)

0x92是Windows-1252的智能引用(')。它根本不存在于unicode中,因此无法解码。

也许您的文件是由Windows机器编辑的,这基本上导致了这个问题?

答案 1 :(得分:4)

使用encoding='cp1252'将解决问题。