Append list of Python dictionaries to a file without loading it

前端 未结 4 934
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-03 11:45

Suppose I need to have a database file consisting of a list of dictionaries:

file:

[
  {\"name\":\"Joe\",\"data\":[1,2,3,4,5]},
  {   ...                         


        
4条回答
  •  时光取名叫无心
    2021-02-03 12:21

    You can use json to dump the dicts, one per line. Now each line is a single json dict that you've written. You loose the outer list, but you can add records with a simple append to the existing file.

    import json
    import os
    
    def append_record(record):
        with open('my_file', 'a') as f:
            json.dump(record, f)
            f.write(os.linesep)
    
    # demonstrate a program writing multiple records
    for i in range(10):
        my_dict = {'number':i}
        append_record(my_dict)
    

    The list can be assembled later

    with open('my_file') as f:
        my_list = [json.loads(line) for line in f]
    

    The file looks like

    {"number": 0}
    {"number": 1}
    {"number": 2}
    {"number": 3}
    {"number": 4}
    {"number": 5}
    {"number": 6}
    {"number": 7}
    {"number": 8}
    {"number": 9}
    

提交回复
热议问题