Save a dictionary to a file (alternative to pickle) in Python?

后端 未结 6 1100
忘掉有多难
忘掉有多难 2020-12-12 12:41

Answered I ended up going with pickle at the end anyway

Ok so with some advice on another question I asked I was told to use pickle to save a dictio

6条回答
  •  难免孤独
    2020-12-12 13:29

    You asked

    Ill give it a shot. How do I specify what file to dump it to/load it from?

    Apart from writing to a string, the json module provides a dump()-method, which writes to a file:

    >>> a = {'hello': 'world'}
    >>> import json
    >>> json.dump(a, file('filename.txt', 'w'))
    >>> b = json.load(file('filename.txt'))
    >>> b
    {u'hello': u'world'}
    

    There is a load() method for reading, too.

提交回复
热议问题