trying to learn shelving keep getting an error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aeristh
    New Member
    • Jul 2013
    • 1

    #1

    trying to learn shelving keep getting an error

    Input:
    Code:
    import pickle, shelve
    
    variety = ["sweet", "hot", "dill"]
    shape = ["whole", "spear", "chip"]
    brand = ["Claussen", "Heinz", "Vlassic"]
    f = open("pickles2.db", "wb")
    pickle.dump(variety, f)
    pickle.dump(shape, f)
    pickle.dump(brand, f)
    f.close()
    
    f = open("pickles2.db", "rb")
    variety = pickle.load(f)
    shape = pickle.load(f)
    brand = pickle.load(f)
    
    print(variety)
    print(shape)
    print(brand)
    f.close()
    
    print("\nShelving lists.")
    s = shelve.open("pickles2.db")
    Output error in idle:

    Traceback (most recent call last):
    File "C:/Python33/pickling 2.py", line 23, in <module>
    s = shelve.open("pi ckles2.db")
    File "C:\Python33\li b\shelve.py", line 232, in open
    return DbfilenameShelf (filename, flag, protocol, writeback)
    File "C:\Python33\li b\shelve.py", line 216, in __init__
    Shelf.__init__( self, dbm.open(filena me, flag), protocol, writeback)
    File "C:\Python33\li b\dbm\__init__. py", line 83, in open
    raise error[0]("db type could not be determined")
    dbm.error: db type could not be determined

    Prints that after outputting everything until after:
    print("\nShelvi ng lists.")

    I'm very new go easy on me.
    Last edited by bvdet; Jul 19 '13, 12:37 PM. Reason: Please use code tags when posting code [code]....[/code]
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    From the Python docs: "A “shelf” is a persistent, dictionary-like object." The data you are attempting to shelve is not a dictionary.

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      Code:
      f = open("pickles2.db", "rb")
      variety = pickle.load(f)
      shape = pickle.load(f)
      brand = pickle.load(f)
      
      s = shelve.open("pickles2.db")
      The pickles2.db file can only be one type, pickle, shelve, text, etc. If you change the name of one of the files, then the program runs, i.e. you are not trying to open a pickle file using shelve.

      Shelve create file example: http://pymotw.com/2/shelve/
      Pickle create file example: http://pymotw.com/2/pickle/index.html

      Comment

      Working...