Итак, я уже сжал свой текст, теперь мне нужно его распаковать, чтобы воссоздать текст.Декомпрессия текстового файла
Компрессия:
import zlib, base64
text = raw_input("Enter a sentence: ")#Asks the user to input text
text = text.split()#Splits the sentence
uniquewords = [] #Creates an empty array
for word in text: #Loop to do the following
if word not in uniquewords: #If the word is not in uniquewords
uniquewords.append(word) #It adds the word to the empty array
positions = [uniquewords.index(word) for word in text] #Finds the positions of each uniqueword
positions2 = [x+1 for x in positions] #Adds 1 to each position
print ("The uniquewords and the positions of the words are: ") #Prints the uniquewords and positions
print uniquewords
print positions2
file = open('task3file.txt', 'w')
file.write('\n'.join(uniquewords))#Adds the uniquewords to the file
file.write('\n')
file.write('\n'.join([str(p) for p in positions2]))
file.close()
file = open('compressedtext.txt', 'w')
text = ', '.join(text)
compression = base64.b64encode(zlib.compress(text,9))
file.write('\n'.join(compression))
print compression
file.close()
Моя попытка декомпрессии:
import zlib, base64
text = ('compressedtext.txt')
file = open('compressedtext.txt', 'r')
print ("In the file is: \n") + file.read()
text = ''.join(text)
data = zlib.decompress(base64.b64decode(text))
recreated = " ".join([uniquewords[word] for word in positions]) #Recreates the sentence
file.close() #Closes the file
print ("The sentences recreated: \n") + recreated
Но когда я бегу декомпрессию и попытаться воссоздать исходный текст появляется сообщение об ошибке сказав
Файл «C: \ Python27 \ lib \ base64.py», строка 77, в b64decode raise TypeError (msg) ТипError: Неверный paddin g
Кто-нибудь знает, как исправить эту ошибку?
Удалить строку 'zlib = []' --- это имя уже было привязано к модулю 'zlib', и вы все равно не используете этот пустой список. –
Теперь он говорит, что zlib не определен – pythonprogrammer
Поместите 'import zlib, base64' сверху? – GramThanos