删除或编辑使用Python泡菜保存的条目

Ale*_*lex 5 python binary pickle

我基本上会执行转储和加载的序列,但是在某些时候我想删除已加载的条目之一。我怎样才能做到这一点?有没有办法删除或编辑用Python pickle / pickckle保存的条目?

编辑:数据用pickle保存在一个二进制文件中。

Bak*_*riu 5

要从二进制文件中删除一个腌制的对象,您必须重写整个文件。该pickle模块不处理流中任意部分的修改,因此没有内置的方式来执行所需的操作。

二进制文件最简单的替代方法可能是使用shelve模块。

dict从文档中的示例可以看到,该模块为包含腌制数据的数据库提供了类似的接口:

import shelve

d = shelve.open(filename) # open -- file may get suffix added by low-level
                          # library

d[key] = data   # store data at key (overwrites old data if
                # using an existing key)
data = d[key]   # retrieve a COPY of data at key (raise KeyError if no
                # such key)
del d[key]      # delete data stored at key (raises KeyError
                # if no such key)
flag = key in d        # true if the key exists
klist = list(d.keys()) # a list of all existing keys (slow!)

# as d was opened WITHOUT writeback=True, beware:
d['xx'] = [0, 1, 2]    # this works as expected, but...
d['xx'].append(3)      # *this doesn't!* -- d['xx'] is STILL [0, 1, 2]!

# having opened d without writeback=True, you need to code carefully:
temp = d['xx']      # extracts the copy
temp.append(5)      # mutates the copy
d['xx'] = temp      # stores the copy right back, to persist it

# or, d=shelve.open(filename,writeback=True) would let you just code
# d['xx'].append(5) and have it work as expected, BUT it would also
# consume more memory and make the d.close() operation slower.

d.close()       # close it
Run Code Online (Sandbox Code Playgroud)

使用的数据库是ndbmgdbm,具体取决于平台和可用的库。

注意:如果数据未移动到其他平台,则此方法效果很好。如果您希望能够将数据库复制到另一台计算机,那么shelve它将不能很好地工作,因为它不能保证将使用哪个库。在这种情况下,使用显式SQL数据库可能是最佳选择。