AttributeError: '_io.StringIO' object has no attribute 'StringIO'

AttributeError: '_io.StringIO' object has no attribute 'StringIO'

我正在寻找这个错误的解决方案,我检查了其他类似的问题但找不到答案。 我正在尝试实施 Lempel-Ziv-1978 数据压缩算法,但在我的解压函数中出现错误:

def 解压器(stringAdecompresser):

from io import StringIO

# creer la dictionnaire
size = 256
dictionnaire = {chr(i): i for i in range(size)}

resultat = StringIO()
w = chr(stringAdecompresser.pop(0))
resultat.write(w)
for k in stringAdecompresser:
    if k in dictionnaire:
        entree = dictionnaire[k]
    else: # k == size:
        entree = w + w[0]


    resultat.write(entree)

    #ajouter dans la dictionnaire
    dictionnaire[size] = w + entree[0]
    size += 1

    w = entree
return resultat.StringIO()

我得到了: 文件 "lz78.py",第 52 行,在解压器中 return resultat.StringIO() AttributeError: '_io.StringIO' 对象没有属性 'StringIO'

Python版本:3.6

您的 resultat 已经是 StringIO。 return 或者,正如@mechanical_meat 在他的评论中建议的那样——resultat.getvalue().