python3报错 name ‘unicode’ is not defined

一份在python2运行正常的代码,在python3运行报错:

 # 首先尝试最常见的预期格式
    if isinstance(date_input, unicode):  # Python 2.7特有的检查
        date_input = date_input.encode('utf-8')  # 转换unicode为str

原来在 Python 3.x 中,字符串默认为 Unicode 字符串,因此不再需要对 unicode 类型进行特殊处理。所以在python3不用对date_input 做特殊处理,删除上面那2行果然运行正常了。

关注公众号“大模型全栈程序员”回复“小程序”获取1000个小程序打包源码。更多免费资源在http://www.gitweixin.com/?p=2627

发表评论

邮箱地址不会被公开。 必填项已用*标注