[Solved] AttributeError: ‘str‘ object has no attribute ‘decode‘

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: