当前位置:   article > 正文

关于python3出现 "AttributeError: 'str' object has no attribute 'decode'"错误的原因及其解决办法_attributeerror: 'str' object has no attribute 'cap

attributeerror: 'str' object has no attribute 'capabilities
  1. /tmp/ python3
  2. Python 3.2.3 (default, Feb 20 2013, 14:44:27)
  3. [GCC 4.7.2] on linux2
  4. Type "help", "copyright", "credits" or "license" for more information.
  5. >>> f1 = open("unicode.txt", 'r').read()
  6. >>> print(f1)
  7. 你好
  8. >>> f2 = open("unicode.txt", 'rb').read() #二进制方式打开
  9. >>> print(f2)
  10. b'\xe5\xaf\x92\xe5\x86\xb7\n'
  11. >>> f2.decode()
  12. '你好\n'
  13. >>> f1.decode()
  14. Traceback (most recent call last):
  15. File "<stdin>", line 1, in <module>
  16. AttributeError: 'str' object has no attribute 'decode'

python 3中只有unicode str,所以把decode方法去掉了。python3环境中,f1已经是unicode str了,不用decode。

如果文件内容不是unicode编码的,要先以二进制方式打开,读入比特流,再解码。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/759151
推荐阅读
相关标签
  

闽ICP备14008679号