gpt4 book ai didi

python - 在 UnicodeError 上显示字符串

转载 作者:太空宇宙 更新时间:2023-11-04 10:43:51 24 4
gpt4 key购买 nike

如果遇到 unicode 错误,有时很难找到问题的根源。这个字符串是从哪里来的?

有没有办法显示字符串(或部分错误字符串)?

最佳答案

您可以使用此代码段:

try:
html = html.decode(encoding)
except UnicodeError as exc:
re_raise_unicode_error_with_hint(exc)

def re_raise_unicode_error_with_hint(exc):
hint = exc.object[max(exc.start - 15, 0):min(exc.end + 15, len(exc.object))]
raise exc.__class__(exc.encoding, exc.object, exc.start, exc.end, 'hint: %r' % hint)

这样您就可以看到字符串的 unicode 错误之前的 15 个字符和之后的 15 个字符。

关于python - 在 UnicodeError 上显示字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18823347/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com