【问题标题】:How to make an exception of "TypeError: coercing to Unicode: need string or buffer, NoneType found"?如何排除“TypeError:强制转换为 Unicode:需要字符串或缓冲区,找到 NoneType”?
【发布时间】:2019-12-06 03:29:56
【问题描述】:

我遇到了这个异常并搜索了网络。 但我无法获得有关如何进行此类例外的任何信息。

有人可以提供一些代码来产生这样的异常吗?

谢谢。

【问题讨论】:

标签: python


【解决方案1】:

您可能正在尝试将 None 隐式转换为 unicode 字符串。我的猜测是您需要在某处进行if var is not None 检查。

$ python2
>>> u"" + None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: coercing to Unicode: need string or buffer, NoneType found

请注意,Python 3.7 会为您提供更好的错误消息:

$ python3
>>> u"" + None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "NoneType") to str

【讨论】:

    猜你喜欢
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 2019-04-02
    • 2021-03-28
    • 2015-02-01
    • 1970-01-01
    相关资源
    最近更新 更多