简体   繁体   中英

How to alter the pickle database in Python?

I have a pickle database which I am reading using the following code

import pickle, pprint
import sys

def main(datafile):
    with open(datafile,'rb')as fin:
        data = pickle.load(fin)

    pprint.pprint(data)

if __name__=='__main__':
    if len(sys.argv) != 2:
        print "Pickle database file must be given as an argument."
        sys.exit()
    main(sys.argv[1])

I recognised that it contained a dictionary. I want to delete/edit some values from this dictionary and make a new pickle database. I am storing the output of this program in a file ( so that I can read the elements in the dictionary and choose which ones to delete) How do I read this file (pprinted data structures) and create a pickle database from it ?

As stated in Python docs pprint is guaranteed to turn objects into valid (in the sense of Python syntax) objects as long as they are representable as Python constants. So first thing is that what you are doing is fine as long as you do it for dicts, lists, numbers, strings, etc. In particular if some value deep down in the dict is not representable as a constant (eg a custom object) this will fail.

Now reading the output file should be quite straight forward:

import ast
with open('output.txt') as fo:
    data = fo.read()
obj = ast.literal_eval(data)

This is assuming that you keep one object per file and nothing more.

Note that you may use built-in eval instead of ast.literal_eval but that is quite unsafe since eval can run arbitrary Python code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM