AttributeError:'str'对象没有属性'decode' Python 3和Python 2.7兼容性

问题描述

我正在尝试比较两个字符串列表:

text_line = [u'\u2502 id               \u2502 na \u2502 email   \u2502',u'\u2502                  \u2502 me \u2502         \u2502']  
print( text_line == [
             "│ id               │ na │ email   │","│                  │ me │         │",])

它发出警告: Unicode警告:Unicode相等比较未能将两个参数都转换为Unicode 显示 False

如果我在列表中每个字符串的末尾添加 .decode('utf-8'),则它在Python 2.7中可以正常工作并显示 True :>

print( text_line == [
       "│ id               │ na │ email   │".decode('utf-8'),"│                  │ me │         │".decode('utf-8'),])

但它在Python 3.X中不起作用,并出现以下错误

AttributeError:'str'对象没有属性'decode'

是否有一种适用于Python 2.7和Python 3.X的方法

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)