【问题标题】:UnicodeDecodeError: 'utf8' codec can't decode byte 0xf6 in position 178175077: invalid start byteUnicodeDecodeError:“utf8”编解码器无法解码位置 178175077 中的字节 0xf6:无效的起始字节
【发布时间】:2016-11-15 11:19:36
【问题描述】:

我正在尝试将 gerrit uid 编码为 utf-8 并遇到以下错误,它大部分时间都可以工作,但对于某些 uid 随机遇到以下错误,我查看了 stackoverflow 上的类似帖子,建议使用 ISO-8859 -1 但 utf-8 对我来说最有效,如何解决?

uid = Ia7324f6443b3db5d55113a221dc0791bb5a38799
uID = gerritInfo['id'].encode("utf-8")

错误堆栈:-

 result=main()
  File "/prj/team/location/script", line 1363, in main
    (picked_gerrit,uID,email_state) = cherrypick_gerrit(buildDir,manifest,gerrit,patch,False,errorLog,picklogfd)
  File "/prj/team/location/script", line 356, in cherrypick_gerrit
    if uID in repo.git.log():
  File "/usr/local/lib/python2.7/dist-packages/git/cmd.py", line 450, in <lambda>
    return lambda *args, **kwargs: self._call_process(name, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/git/cmd.py", line 902, in _call_process
    return self.execute(make_call(), **_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/git/cmd.py", line 698, 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 0xf6 in position 178175077: invalid start byte

【问题讨论】:

  • 堆栈跟踪与您发布的代码行无关。
  • 为什么这么说?它在uID in repo.git.log(): 失败repo.git.log() 是一个python 模块http://gitpython.readthedocs.io/en/stable/tutorial.html,我提供了一个失败的uID 示例
  • 堆栈跟踪指示 解码 错误。 0xf6 作为独立字节或多字节字符的开头无效。在第 1363 行附近向我们展示 /prj/team/location/script。
  • 第 1363 行看起来像 for gerritPatch in eCRMgerritList: (gerrit, sep, patch) = gerritPatch.partition(r'/') (picked_gerrit,uID,email_state) = cherrypick_gerrit(buildDir,manifest,gerrit,patch,False,errorLog,picklogfd)
  • 我完全不知道如何调试 if uID in repo.git.log(): 会抛出这样的错误,非常感谢任何帮助或指向 DEBUG 的指针

标签: python git utf-8 decoding gitpython


【解决方案1】:

在 python 文件的第一行添加# coding=utf-8

【讨论】:

  • 杰瑞 - 这有什么帮助?我已经在做uID = gerritInfo['id'].encode("utf-8")
  • 执行 print(gerritInfo['id']) 结果是什么
  • Ia7324f6443b3db5d55113a221dc0791bb5a38799 是结果,您希望看到任何特殊字符吗?
  • gerritInfo['id'] 的结果可能不是字符串类型的对象尝试将其转换为字符串 str(gerritInfo['id']).encode("utf-8")跨度>
【解决方案2】:

基于https://github.com/gitpython-developers/GitPython/issues/237,你可以试试下面的方法,使用stdout_as_string=False

if uID in repo.git.log(stdout_as_string=False)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-08
    • 2013-10-02
    • 2016-11-25
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多