python - 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 │ │",
])
它给出警告:UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode and prints 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 的方法?
解决方案
推荐阅读
- php - Woocommerce 表单没有动作?
- python - 尝试使用 Discord.py 在 Atom 中制作我自己的 Discord Bot,但它没有连接并出现许多错误
- python - 在sql查询Python中传递一个元素作为参数的元组
- python - 在 Python 中 - 尝试在 B 列中聚合相同的名称,并从 C 列中的每个总数中求和它们的总数
- python - 如何将哪个命令的结果通过管道传输到子进程中的另一个命令?
- xcode - 如何在 SpriteKit 中更改精灵的触摸锚点
- macos - iTerm2 中的自定义 alt-tab 应用程序切换器没有响应
- sql - 消息 156,级别 15,状态 1,第 171 行关键字“FROM”附近的语法不正确
- java - 用一维数组而不是二维数组创建一个“挖掘”游戏?
- angularjs - 如何动态修剪空间?