I was encoding a simple text file using LZW algorithm in python. However, i realised that i can only write a string to a .txt
file using the write() function, which itself occupies almost as much space. So is it possible to somehow just write actual integers to a file (maybe in a different format),
to achieve proper compression ?
readfile = open("C:/Users/Dhruv/Desktop/read.txt", "r")
writefile = open("C:/Users/Dhruv/Desktop/write.txt", "w")
content = readfile.read()
length = len(content)
codes = []
for i in range(0, 256) :
codes.append(str(chr(i)))
current_string = ""
for i in range(0, length) :
temp = current_string + content[i]
print(temp)
if temp in codes :
current_string += content[i]
else :
codes.append(current_string + content[i])
writefile.write(str(codes.index(current_string)) + " ")
current_string = str(content[i])
writefile.write(str(codes.index(current_string)) + " ")
readfile.close()
writefile.close();