Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
41 lines (41 sloc) 1.02 KB
import re
from ast import literal_eval
import os
def compress():
try:
fileName = input("Enter the name of the file to be compressed: ")
file = open(fileName)
text = file.read()
file.close()
p = re.compile(r'[\w]+|[\W]')
split = p.findall(text)
b = []
wordList = []
for word in split:
try:
r = wordList.index(word) + 1
except ValueError:
wordList.append(word)
r = len(wordList)
b.append(r)
file = open('compressed.txt', 'w')
file.write(str(wordList)+'\n'+str(b))
file.close()
except:
print("File does not exist")
def decompress():
try:
fileName = input("Enter the name of the file to be decompressed: ")
file = open(fileName)
except:
print("File does not exist")
print("Contents of the compressed file is: ")
words = literal_eval(file.readline().rstrip('\n'))
pos = literal_eval(file.readline())
temp = []
for index in pos:
temp.append(words[index-1])
sentence = ''.join(temp)
print(sentence)
compress()
decompress()
You can’t perform that action at this time.