Python:修改pickle对象

时间:2011-07-28 08:03:38

标签: python pickle

我正在使用cPickle来存储对象。后来我想修改个人记录。不幸的是,修改后文件被破坏了(我得到了UnpicklingError):

创建和Pickle

class Book(object):
    def __init__(self, title, author, ISBN, price):
        self.title = title
        self.author = author
        self.ISBN = ISBN
        self.price = price

def main():
    from cPickle import dump
    from random import choice
    from random import randrange
    letters = ("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
           "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
           "w", "x", "y", "z")
    f = open("books.dat", "a+b")

    for ctr in range(0, 20):
        title = ""
        author = ""
        ISBN = ""
        price = randrange(20,100)
        for i in range(10):
            title+=choice(letters)
            author+=choice(letters)
            ISBN+=str(randrange(0,14))
        book = Book(title, author, ISBN, price)
        # writing structure
        dump(book, f)
    f.close()
    print "Finished."

main()

unPickle并修改

class Book(object):
    def __init__(self, title, author, ISBN, price):
        self.title = title
        self.author = author
        self.ISBN = ISBN
        self.price = price

    def __str__(self):
        rep = self.title+" by "+self.author+".\n"
        rep += "ISBN: "+self.ISBN+"\n"
        rep += "Price: "+str(self.price)
        return rep

def main():
    from cPickle import load
    from cPickle import dump
    import sys

    books = []
    EOF = False
    f = open("books.dat", "r+b")
    print "Loading..."
    while not EOF:
        try:
            book = load(f)
        except(EOFError):
            EOF = True
        else:
            books.append(book)
    print "Load complete."
    rec = int(raw_input("Record to delete: "))
    print books[rec]
    fwd = 0
    for i in range(rec):
        fwd+= sys.getsizeof(books[i]) 
    print str(fwd)
    title = "New"
    author = "New"
    ISBN = "New"
    price = 0
    new = Book(title, author, ISBN, price)
    print str(sys.getsizeof(new))
    f.seek(fwd)
    dump(new, f)
    f.close()

main()

1 个答案:

答案 0 :(得分:2)

我不确定,但在我看来,因为你已经有一个书籍对象列表(从文件加载)所以如果你用这个列表的上下文重写你的文件,它会更容易和没有错误(您可以使用转储删除您想要的记录或只是书籍[i] = new),而不是试图找到插入新记录的地方。

换句话说,请尝试以下方法:

class Book(object):

    def __init__(self, title, author, ISBN, price):
        self.title = title
        self.author = author
        self.ISBN = ISBN
        self.price = price

    def __str__(self):
        rep = self.title+" by "+self.author+".\n"
        rep += "ISBN: "+self.ISBN+"\n"
        rep += "Price: "+str(self.price)
        return rep

def main():
    from cPickle import load, dump

    print "Loading..."
    books = [load(line) for line in open("books.dat", "rb")]
    print "Load complete."

    rec = int(raw_input("Record to delete: "))        
    title = "New"
    author = "New"
    ISBN = "New"
    price = 0

    books[rec] = Book(title, author, ISBN, price) # replace previous record

    with open("books.dat", "wb") as f:        
        for record in books:
            dump(record, f)


main()