Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def compress(uncompressed):
- dict_size = 256
- dictionary = {chr(i): i for i in range(dict_size)}
- w = ""
- result = []
- for c in uncompressed:
- wc = w + c
- if wc in dictionary:
- w = wc
- else:
- result.append(dictionary[w])
- dictionary[wc] = dict_size
- dict_size += 1
- w = c
- if w:
- result.append(dictionary[w])
- return result
- def decompress(compressed):
- from io import StringIO
- dict_size = 256
- dictionary = {i: chr(i) for i in range(dict_size)}
- result = StringIO()
- w = chr(compressed.pop(0))
- result.write(w)
- for k in compressed:
- if k in dictionary:
- entry = dictionary[k]
- elif k == dict_size:
- entry = w + w[0]
- else:
- raise ValueError('Bad compressed k: %s' % k)
- result.write(entry)
- dictionary[dict_size] = w + entry[0]
- dict_size += 1
- w = entry
- return result.getvalue()
- with open('test.json', 'r') as f:
- data = f.read()
- removal_list = [' ', '\t', '\n']
- for s in removal_list:
- data = data.replace(s, '')
- print(data)
- compressed = compress(data)
- print(compressed)
- print(compressed.__sizeof__())
- decompressed = decompress(compressed)
- print(decompressed)
- print(decompressed.__sizeof__())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement