Search code examples
pythonpython-2.7dictionarytrace

Error in loading large(16MB) python dictionary


I created a python dictionary earlier and saved it file in text format. The size of the file is 16MB. When I try to load it using ast

f = "dictionaryInTextFile"
fileToRead = open(f, 'r')
Object = fileToRead.read()
fileToRead.close()

ObjectDict = ast.literal_eval(Object)

I get the following error

eTraceback (most recent call last):
File "somename.py", line 46, in <module>
   ObjectDict = ast.literal_eval(Object)
File "/usr/lib/python2.7/ast.py", line 49, in literal_eval
   node_or_string = parse(node_or_string, mode='eval')
File "/usr/lib/python2.7/ast.py", line 37, in parse
   return compile(source, filename, mode, PyCF_ONLY_AST)
File "<unknown>", line 1

Although when I tried to load a portion of the dictionary, it loaded successfully.

Is there a problem with the size of the dictionary or is there a some problem while parsing the dictionary from the text file and what can I do to solve it?


Solution

  • No, there is not a specific size limitation on dictionaries loaded by ast.literal_eval.

    There are, however, limitations on the keys and values that the dictionary may contain. Quoting the documentation:

    The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.

    I suppose that your dictionary contains something other than the above types.

    As near as you can, divide your file in half, adding whatever punctuation is required to ensure that both halves are still valid dictionaries. Run ast.literal_eval() against both halves. If one of them raises an exception, cut it in half and repeat the process. Soon enough you'll discover the location of the problem.