Original code:
def loadTxt(filenameTxt):
txtList = [line.strip().decode('utf-8') for line in open(filenameTxt,'r').readlines()]#变成 unicode
return txtList#unicode
report errors:
AttributeError: ‘str‘ object has no attribute ‘decode‘
Solution: decode the byte string
def loadTxt(filenameTxt):
txtList = [line.strip().encode('utf-8').decode('utf-8') for line in open(filenameTxt,'r').readlines()]#变成 unicode
return txtList#unicode
Read More:
- [Solved] AttributeError: ‘str‘ object has no attribute ‘decode‘
- Python 3.7 Error: AttributeError: ‘str‘ object has no attribute ‘decode‘ [How to Solve]
- Djangorestframework-simplejwt: ‘str‘ object has no attribute ‘decode‘ [Solved]
- [Solved] Pytorch-transformers Error: AttributeError: ‘str‘ object has no attribute ‘shape‘
- AttributeError str object has no attribut [How to Solve]
- [Solved] AttributeError: ‘PngImageFile‘ object has no attribute ‘imshow‘
- [Solved] vpython: AttributeError: ‘box‘ object has no attribute ‘idx‘
- [Solved] AttributeError: ‘DataParallel‘ object has no attribute ‘save‘
- [Solved] AttributeError: ‘NoneType‘ object has no attribute ‘astype‘
- [Solved] AttributeError: ‘NoneType‘ object has no attribute ‘append‘
- [Solved] AttributeError: ‘DataFrame‘ object has no attribute ‘map‘
- AttributeError: DatetimeProperties object has no attribute
- [Solved] AttributeError: ‘DataFrame‘ object has no attribute ‘tolist‘
- How to Solve Python AttributeError: ‘dict’ object has no attribute ‘item’
- [Solved] AttributeError: ‘HTMLWriter‘ object has no attribute ‘_temp_names‘
- [Solved] Pycharm error: attributeerror: ‘Htmlparser’ object has no attribute ‘unescape’
- [Solved] AttributeError WriteOnlyWorksheet object has no attribute cell
- [Solved] Networkx Error: Attributeerror: ‘graph’ object has no attribute ‘node’
- [Solved] AttributeError: DataFrame object has no attribute’xxx’
- [Modified] AttributeError: ‘socket‘ object has no attribute ‘ioctl‘ python linux