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

8 windows git encoding github 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 
Run Code Online (Sandbox Code Playgroud)

相关代码:

..................
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
Run Code Online (Sandbox Code Playgroud)

Exc*_*een 15

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

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

  • Windows-1252中的0x92只是Unicode中[U + 2019 RIGHT SINGLE QUOTATION MARK](http://codepoints.net/U+2019)代码点的一种编码.声明它在Unicode中不存在是...不正确; 在UTF8中,它应编码为0xE2 0x80 0x99.如果*是关于编码为CP-1252的数据,那么这只意味着某处存在编码错误. (8认同)

Abd*_*man 10

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