Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def write_to_file(filename, packed_data, huffman_dict, step, min_value, length):
- with open(filename, "wb") as f:
- f.write(str(step).encode("utf-8") + b"\n")
- f.write(str(min_value).encode("utf-8") + b"\n")
- f.write(str(length).encode("utf-8") + b"\n")
- f.write(packed_data + b"\n")
- f.write("*".join([str(element) for element in list(huffman_dict.keys())]).encode("utf-8") + b"\n")
- f.write("*".join(list(huffman_dict.values())).encode("utf-8") + b"\n")
- def read_from_file(filename):
- with open(filename, "rb") as f:
- lines = f.readlines()
- for i in lines:
- print(i.strip())
- step = float(lines[0].decode("utf-8").strip())
- min_value = float(lines[1].decode("utf-8").strip())
- length = int(lines[2].decode("utf-8").strip())
- packed_data = lines[3].strip()
- keys = lines[4].strip().decode("utf-8").split("*")
- values = lines[5].strip().decode("utf-8").split("*")
- huffman_dict = dict(zip(keys, values))
- for i in range(4, len(lines), 2):
- try:
- key = lines[i].strip().decode("utf-8")
- except UnicodeDecodeError:
- print("\nUnicodeDecodeError, Values\n")
- key = lines[i].strip().decode("utf-8", errors='ignore')
- #key = lines[i].strip()
- try:
- key = int(key)
- #except ValueError:
- except:
- pass
- try:
- value = lines[i+1].decode("utf-8").strip()
- except UnicodeDecodeError:
- print("\nUnicodeDecodeError, Values\n")
- value = lines[i+1].decode("utf-8", errors='ignore').strip()
- huffman_dict[key] = value
- lines = list(f)
- step = float(lines[0].decode("utf-8").strip())
- min_value = float(lines[1].decode("utf-8").strip())
- length = int(lines[2].decode("utf-8").strip())
- packed_data = lines[3].strip()
- huffman_dict = pickle.loads(lines[4].strip())
- return packed_data, huffman_dict, step, min_value, length
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement