尝试了很多方法后,我经常遇到很多类型的错误。这里是我现在的代码,我需要能够压缩到一个文件。压缩字符串和列表:类型错误
import zlib
sentence = input("Enter the text you want to compress: ")
listSentence = sentence.split(" ")
d = {}
i = 0
values = []
for i, word in enumerate(sentence.split(" ")):
if not word in d:
d[word] = (i+1)
values += [d[word]]
coms = zlib.compress(sentence.encode('utf-8'))
comv = zlib.compress(values.encode('utf-8'))
with open("listofwords.txt", "wb") as myfile:
myfile.write(coms)
myfile.write(comv)
我不断收到一个错误类型: Type error: List does not support the buffer interface
任何援助将不胜感激!
当您想要帮助发生错误时,您总是需要发布回溯。 – thebjorn
好吧,就像实际的错误说的那样,'values'是一个列表,你不能编码一个列表。你想达到什么目的?请注意,在您的代码中,“值”只是从1到您句子中唯一字数的整数列表。 –
@DanielRoseman值和输入都需要压缩。 – Trent