Python: How do I write a list to file and then pull it back into memory (dict represented as a string convert to dict) later?

后端 未结 6 2015
遥遥无期
遥遥无期 2020-12-23 11:48

More specific dupe of 875228—Simple data storing in Python.

I have a rather large dict (6 GB) and I need to do some processing on it. I\'m trying out several docume

6条回答
  •  旧巷少年郎
    2020-12-23 12:27

    I'd use shelve, json, yaml, or whatever, as suggested by other answers.

    shelve is specially cool because you can have the dict on disk and still use it. Values will be loaded on-demand.

    But if you really want to parse the text of the dict, and it contains only strings, ints and tuples like you've shown, you can use ast.literal_eval to parse it. It is a lot safer, since you can't eval full expressions with it - It only works with strings, numbers, tuples, lists, dicts, booleans, and None:

    >>> import ast
    >>> print ast.literal_eval("{12: 'mydict', 14: (1, 2, 3)}")
    {12: 'mydict', 14: (1, 2, 3)}
    

提交回复
热议问题