Thread: cPickle module

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12

    cPickle module


    Hi,
    In case of pickling and unpickling using the cPickle module, can we load a file which contains mixed objects...i.e lists and dictionaries???? Or is there a way to do it??

    Thanks & Rgds,
    Subha
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    yes,

    Most objects can be pickled and dictionaries and lists containing dictionaries and lists and mixtures of both are fine.
    From the documentation:
    3.14.4 What can be pickled and unpickled?
    ...
    tuples, lists, and dictionaries containing only picklable objects
    grim
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    Hey Grim,
    Thanks for that information but I know that such things can happen
    My query was a bit different...I've got the answer just now

    Code:
    >>> Scilist=['Einstein','Copernicus','Pascal']
    >>> PrgDict={'A':'Ada','B':'Basic','C':'Cobol','P':'Python'}
    then I'm pickling it...

    Code:
    >>> cPickle.dump(Scilist,myFile)
    >>> cPickle.dump(PrgDict,myFile)
    Now unpickling it.....

    Code:
    >>> cPickle.load(myFile)
    ['Einstein', 'Copernicus', 'Pascal']
    >>> cPickle.load(myFile)
    {'A': 'Ada', 'P': 'Python', 'C': 'Cobol', 'B': 'Basic'}
    The thing was I thought on doing cPickle.load(myFile) I wld be able to get all the contents of the file...which was wrong... is there a way to do it in one shot??

    Thanks,
    Subha
  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    I think not directly but you can do this...
    Code:
    Scilist=['Einstein','Copernicus','Pascal'] 
    PrgDict={'A':'Ada','B':'Basic','C':'Cobol','P':'Python'}
    
    cPickle.dump([Scilist,PrgDict],myFile)
    
    Scilist,PrgDict = cPickle.load(myFile)
    grim
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12
    Hey Grim,
    That was great! Thanks a lot!

    Subha

IMN logo majestic logo threadwatch logo seochat tools logo