Advertisement
Oppaceted

Untitled

Dec 10th, 2024
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. def write_to_file(filename, packed_data, huffman_dict, step, min_value, length):
  2. with open(filename, "wb") as f:
  3. f.write(str(step).encode("utf-8") + b"\n")
  4. f.write(str(min_value).encode("utf-8") + b"\n")
  5. f.write(str(length).encode("utf-8") + b"\n")
  6. f.write(packed_data + b"\n")
  7.  
  8. f.write("*".join([str(element) for element in list(huffman_dict.keys())]).encode("utf-8") + b"\n")
  9. f.write("*".join(list(huffman_dict.values())).encode("utf-8") + b"\n")
  10.  
  11. def read_from_file(filename):
  12. with open(filename, "rb") as f:
  13. lines = f.readlines()
  14. for i in lines:
  15. print(i.strip())
  16. step = float(lines[0].decode("utf-8").strip())
  17. min_value = float(lines[1].decode("utf-8").strip())
  18. length = int(lines[2].decode("utf-8").strip())
  19. packed_data = lines[3].strip()
  20. keys = lines[4].strip().decode("utf-8").split("*")
  21. values = lines[5].strip().decode("utf-8").split("*")
  22. huffman_dict = dict(zip(keys, values))
  23.  
  24. for i in range(4, len(lines), 2):
  25. try:
  26. key = lines[i].strip().decode("utf-8")
  27. except UnicodeDecodeError:
  28. print("\nUnicodeDecodeError, Values\n")
  29. key = lines[i].strip().decode("utf-8", errors='ignore')
  30. #key = lines[i].strip()
  31.  
  32. try:
  33. key = int(key)
  34. #except ValueError:
  35. except:
  36. pass
  37.  
  38. try:
  39. value = lines[i+1].decode("utf-8").strip()
  40. except UnicodeDecodeError:
  41. print("\nUnicodeDecodeError, Values\n")
  42. value = lines[i+1].decode("utf-8", errors='ignore').strip()
  43. huffman_dict[key] = value
  44.  
  45. lines = list(f)
  46. step = float(lines[0].decode("utf-8").strip())
  47. min_value = float(lines[1].decode("utf-8").strip())
  48. length = int(lines[2].decode("utf-8").strip())
  49. packed_data = lines[3].strip()
  50. huffman_dict = pickle.loads(lines[4].strip())
  51. return packed_data, huffman_dict, step, min_value, length
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement