I have an incredibly large dict of the shape:
{'rounding': (4, [(900, 1), (4406, 0), (5772, 1), (6210, 1)]), 'thee': (5, [(901, 1), (3452, 1), (3803, 1), (4178, 1), (5793, 1)]), 'hotdog': (13, [(902, 2), (902, 2), (996, 1), (1765, 1), (2602, 1), (3824, 1), (4701, 1), (4924, 1), (5544, 1), (5741, 1), (5984, 1), (6972, 1), (7236, 2), (7236, 2), (7469, 1)]), 'hotdogs': (9, [(902, 1), (1765, 2), (1765, 2), (4924, 0), (5110, 1), (5228, 1), (6883, 1), (7034, 1), (7236, 1), (8638, 1)]),}
It continues on and on for about ~450k terms or so. It is a very large dict. I need to write this object to a binary file. I am following these resources:
- Python struct.error: bad char in struct format
- Python struct.pack() 'struct.error: bad char in struct format' when trying to save endianess
- struct.error: bad char in struct format
The code that is resulting in an error is (the whole program is a few thousand lines):
inverted_index = {word:(document_frequency[word], d[word]) for word in d}
json_dict = json.dumps(inverted_index)
struct.pack(json_dict)
Which yields the error
File "take_7.py", line 179, in <module>
main()
File "take_7.py", line 176, in main
driver(sys.argv[1])
File "take_7.py", line 164, in driver
struct.pack('i', json_dict)
struct.error: bad char in struct format
I tried looking up struct documentation and then tried:
inverted_index = {word:(document_frequency[word], d[word]) for word in d}
json_dict = json.dumps(inverted_index)
binary_file = struct.pack('s', bytes(json_dict, 'utf-8'))
Which compiled. However:
print(binary_file) yields b'{'
And
print(struct.unpack('s', binary_file)) yields (b'{',)
How can I convert my dict
(as described above) to a binary file, so that I can save it to disk, and later read it back from disk to be used?