2016-07-12 5 views
0

Итак, я уже сжал свой текст, теперь мне нужно его распаковать, чтобы воссоздать текст.Декомпрессия текстового файла

Компрессия:

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

Кто-нибудь знает, как исправить эту ошибку?

+0

Удалить строку 'zlib = []' --- это имя уже было привязано к модулю 'zlib', и вы все равно не используете этот пустой список. –

+0

Теперь он говорит, что zlib не определен – pythonprogrammer

+0

Поместите 'import zlib, base64' сверху? – GramThanos

ответ

2

Здесь есть несколько вещей. Позвольте мне начать, давая вам рабочий образец:

import zlib, base64 

rawtext = raw_input("Enter a sentence: ") # Asks the user to input text 
text = rawtext.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 

infile = open('task3file.txt', 'w') 
infile.write('\n'.join(uniquewords)) # Adds the uniquewords to the file 
infile.write('\n') 
infile.write('\n'.join([str(p) for p in positions2])) 
infile.close() 

infile = open('compressedtext.b2', 'w') 

compression = base64.b64encode(zlib.compress(rawtext, 9)) 

infile.write(compression) 

print compression 

infile.close() 

# Now read it again 

infile = open('compressedtext.b2', 'r') 
text = infile.read() 
print("In the file is: " + text) 
recreated = zlib.decompress(base64.b64decode(text)) 
infile.close() 
print("The sentences recreated:\n" + recreated) 

Я пытался держать вещи довольно близко к тому, что у вас было, но обратите внимание, в частности, несколько изменений:

  • Я пытаюсь для более тщательного отслеживания исходного текста и обработанного текста .

  • Я удалил переопределение zlib.

  • Я удалил лишние разрывы строки, которые разрушили декомпрессию.

  • Я сделал некоторую общую очистку, чтобы лучше соответствовать нормальным соглашениям Python .

Надеюсь, это поможет.

+0

Да помогает много спасибо! – pythonprogrammer