0

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();
Dhruv Chadha
  • 1,161
  • 2
  • 11
  • 33

1 Answers1

1

If your data can be represented as Numpy array, the following function can write it as integer in a .txt file:

import numpy as np
def writer(_hd, _data):
    out_file_name = str(_hd) + '.csv'
    np.savetxt(out_file_name, _data, fmt='%i')
    return None

where _hd is the file name and _data is the numpy array. The fmt='%i' saves the data as integer; Other options are also available here.

salehinejad
  • 7,258
  • 3
  • 18
  • 26