python 3で「AttributeError:'str'object has no attribute'decode」というエラーが発生した原因とその解決方法について
/tmp/ python3
Python 3.2.3 (default, Feb 20 2013, 14:44:27)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f1 = open("unicode.txt", 'r').read()
>>> print(f1)
>>> f2 = open("unicode.txt", 'rb').read() #
>>> print(f2)
b'\xe5\xaf\x92\xe5\x86\xb7
'
>>> f2.decode()
'
'
>>> f1.decode()
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'str' object has no attribute 'decode'
python 3にはunicode strしかないのでdecodeメソッドを削除しました.python 3環境では、f 1はすでにunicode strであり、decodeは使用されません.ファイルの内容がunicode符号化されていない場合は、まずバイナリ方式で開き、ビットストリームを読み込み、復号します.