save dictionary for later use?

Discussion in 'Python' started by globalrev, May 16, 2008.

  1. globalrev

    globalrev Guest

    i extract info from one file and put it into a dictionary.
    i want to save that dictionary for later use, how do i do that?
    might save a list of dictionaries or a list of classobjects too if
    there is any difference.
     
    globalrev, May 16, 2008
    #1
    1. Advertising

  2. globalrev

    jay graves Guest

    On May 16, 2:17 pm, globalrev <> wrote:
    > i extract info from one file and put it into a dictionary.
    > i want to save that dictionary for later use, how do i do that?
    > might save a list of dictionaries or a list of classobjects too if
    > there is any difference.


    use the 'pickle' module.
    http://docs.python.org/lib/module-pickle.html

    ....
    Jay Graves
     
    jay graves, May 16, 2008
    #2
    1. Advertising

  3. globalrev

    globalrev Guest

    On 16 Maj, 21:22, jay graves <> wrote:
    > On May 16, 2:17 pm, globalrev <> wrote:
    >
    > > i extract info from one file and put it into a dictionary.
    > > i want to save that dictionary for later use, how do i do that?
    > > might save a list of dictionaries or a list of classobjects too if
    > > there is any difference.

    >
    > use the 'pickle' module.http://docs.python.org/lib/module-pickle.html
    >
    > ...
    > Jay Graves



    pickle.dumps(mg)
    pickle.load(mg)

    'dict' object has no attribute 'readline'
    dumps load(well i dont know but i get no complaint but running load
    generates that error)
     
    globalrev, May 16, 2008
    #3
  4. globalrev

    jay graves Guest

    On May 16, 3:24 pm, globalrev <> wrote:
    > On 16 Maj, 21:22, jay graves <> wrote:
    > > On May 16, 2:17 pm, globalrev <> wrote:
    > > > i extract info from one file and put it into a dictionary.
    > > > i want to save that dictionary for later use, how do i do that?
    > > > might save a list of dictionaries or a list of classobjects too if
    > > > there is any difference.

    > > use the 'pickle' module.http://docs.python.org/lib/module-pickle.html


    > pickle.dumps(mg)
    > pickle.load(mg)
    >
    > 'dict' object has no attribute 'readline'
    > dumps load(well i dont know but i get no complaint but running load
    > generates that error)


    It's best to post a minimal set of code that exhibits your error.
    You aren't saving the output of pickle.dumps and you are using
    pickle.load instead of pickle.loads.

    Sample loading to and from a string which you can tuck away in a file.

    >>> import pickle
    >>> test = {'a':1,'b':2}
    >>> picklestr = pickle.dumps(test)
    >>> test2 = pickle.loads(picklestr)
    >>> test == test2

    True
    >>>



    Sample using an open file.

    >>> import pickle
    >>> test = {'a':1,'b':2}
    >>> pfile = open('pickletest','wb')
    >>> pickle.dump(test,pfile)
    >>> pfile.close()
    >>> pfile = open('pickletest','rb')
    >>> test2 = pickle.load(pfile)
    >>> pfile.close()
    >>> test == test2

    True
    >>>


    ....
    Jay Graves
     
    jay graves, May 16, 2008
    #4
  5. globalrev

    Hans Nowak Guest

    globalrev wrote:

    > pickle.dumps(mg)
    > pickle.load(mg)
    >
    > 'dict' object has no attribute 'readline'
    > dumps load(well i dont know but i get no complaint but running load
    > generates that error)


    The 'loads' and 'dumps' methods use strings:

    >>> import pickle
    >>> d = {"this": 42, "that": 101, "other": 17}
    >>> s = pickle.dumps(d)
    >>> s

    "(dp0\nS'this'\np1\nI42\nsS'other'\np2\nI17\nsS'that'\np3\nI101\ns."
    >>> pickle.loads(s)

    {'this': 42, 'other': 17, 'that': 101}

    If you want to store to / restore from file, use 'dump' and 'load':

    # write to file 'out'...
    >>> f = open("out")
    >>> f = open("out", "wb")
    >>> pickle.dump(d, f)
    >>> f.close()


    # restore it later
    >>> g = open("out", "rb")
    >>> e = pickle.load(g)
    >>> g.close()
    >>> e

    {'this': 42, 'other': 17, 'that': 101}

    Also see http://docs.python.org/lib/pickle-example.html.

    Hope this helps!

    --Hans
     
    Hans Nowak, May 16, 2008
    #5
  6. globalrev

    Guest

    On 16 mai, 22:24, globalrev <> wrote:
    > On 16 Maj, 21:22, jay graves <> wrote:
    >
    > > On May 16, 2:17 pm, globalrev <> wrote:

    >
    > > > i extract info from one file and put it into a dictionary.
    > > > i want to save that dictionary for later use, how do i do that?
    > > > might save a list of dictionaries or a list of classobjects too if
    > > > there is any difference.

    >
    > > use the 'pickle' module.http://docs.python.org/lib/module-pickle.html

    >
    > > ...
    > > Jay Graves

    >
    > pickle.dumps(mg)
    > pickle.load(mg)
    >
    > 'dict' object has no attribute 'readline'
    > dumps load(well i dont know but i get no complaint but running load
    > generates that error)


    What about *READING THAT FUCKING MANUAL* ?

    http://docs.python.org/lib/node316.html
    """
    dump(obj, file[, protocol])
    Write a pickled representation of obj to the open file object
    file. This is equivalent to Pickler(file, protocol).dump(obj).

    If the protocol parameter is omitted, protocol 0 is used. If
    protocol is specified as a negative value or HIGHEST_PROTOCOL, the
    highest protocol version will be used.

    Changed in version 2.3: Introduced the protocol parameter.

    file must have a write() method that accepts a single string
    argument. It can thus be a file object opened for writing, a StringIO
    object, or any other custom object that meets this interface.

    load(file)
    Read a string from the open file object file and interpret it as a
    pickle data stream, reconstructing and returning the original object
    hierarchy. This is equivalent to Unpickler(file).load().

    file must have two methods, a read() method that takes an integer
    argument, and a readline() method that requires no arguments. Both
    methods should return a string. Thus file can be a file object opened
    for reading, a StringIO object, or any other custom object that meets
    this interface.

    This function automatically determines whether the data stream was
    written in binary mode or not.
    """

    Example use:

    >>> d = dict(a=1, b=2)
    >>> f = open("mydict.dat", "w")
    >>> pickle.dump(d, f)
    >>> f.close()
    >>> f = open("mydict.dat")
    >>> d2 = pickle.load(f)
    >>> f.close()
    >>> d2 == d

    True
    >>> d2

    {'a': 1, 'b': 2}
    >>>


    Now : it's not the first time - in a couple days - that you ask a
    question that you wouldn't have asked if you had taken a couple
    minutes doing the tutorial and/or reading the doc. This newsgroup is
    *very* tolerant (in most other places on usenet, you would have get a
    raw RTFM on the first question, and a PLONK on the second), but there
    are still limits, and as far as I'm concerned you're not far from
    them. So do yourself and the world a favour, read this:
    http://catb.org/~esr/faqs/smart-questions.html

    and then this:
    http://docs.python.org/tut/tut.html

    and next time someone points you to a specific module, have mercy and
    *read* the doc before posting.

    As far as I'm concerned, I won't anwser any of your questions unless
    it's obvious that you have followed these advices (but then I'll be
    happy to help if I can).
     
    , May 16, 2008
    #6
  7. globalrev

    castironpi Guest

    On May 16, 4:29 pm, ""
    <> wrote:
    > On 16 mai, 22:24, globalrev <> wrote:
    >
    >
    >
    >
    >
    > > On 16 Maj, 21:22, jay graves <> wrote:

    >
    > > > On May 16, 2:17 pm, globalrev <> wrote:

    >
    > > > > i extract info from one file and put it into a dictionary.
    > > > > i want to save that dictionary for later use, how do i do that?
    > > > > might save a list of dictionaries or a list of classobjects too if
    > > > > there is any difference.

    >
    > > > use the 'pickle' module.http://docs.python.org/lib/module-pickle.html

    >
    > > > ...
    > > > Jay Graves

    >
    > > pickle.dumps(mg)
    > > pickle.load(mg)

    >
    > > 'dict' object has no attribute 'readline'
    > > dumps load(well i dont know but i get no complaint but running load
    > > generates that error)

    >
    > What about *READING THAT FUCKING MANUAL* ?
    >
    > http://docs.python.org/lib/node316.html
    > """
    > dump(obj, file[, protocol])
    >     Write a pickled representation of obj to the open file object
    > file. This is equivalent to Pickler(file, protocol).dump(obj).
    >
    >     If the protocol parameter is omitted, protocol 0 is used. If
    > protocol is specified as a negative value or HIGHEST_PROTOCOL, the
    > highest protocol version will be used.
    >
    >     Changed in version 2.3: Introduced the protocol parameter.
    >
    >     file must have a write() method that accepts a single string
    > argument. It can thus be a file object opened for writing, a StringIO
    > object, or any other custom object that meets this interface.
    >
    > load(file)
    >     Read a string from the open file object file and interpret it as a
    > pickle data stream, reconstructing and returning the original object
    > hierarchy. This is equivalent to Unpickler(file).load().
    >
    >     file must have two methods, a read() method that takes an integer
    > argument, and a readline() method that requires no arguments. Both
    > methods should return a string. Thus file can be a file object opened
    > for reading, a StringIO object, or any other custom object that meets
    > this interface.
    >
    >     This function automatically determines whether the data stream was
    > written in binary mode or not.
    > """
    >
    > Example use:
    >
    >
    >
    > >>> d = dict(a=1, b=2)
    > >>> f = open("mydict.dat", "w")
    > >>> pickle.dump(d, f)
    > >>> f.close()
    > >>> f = open("mydict.dat")
    > >>> d2 = pickle.load(f)
    > >>> f.close()
    > >>> d2 == d

    > True
    > >>> d2

    > {'a': 1, 'b': 2}
    >
    > Now : it's not the first time - in a couple days - that you ask a
    > question that you wouldn't have asked if you had taken a couple
    > minutes doing the tutorial and/or reading the doc. This newsgroup is
    > *very* tolerant (in most other places on usenet, you would have get a
    > raw RTFM on the first question, and a PLONK on the second), but there
    > are still limits, and as far as I'm concerned you're not far from
    > them. So do yourself and the world a favour, read this:http://catb.org/~esr/faqs/smart-questions.html
    >
    > and then this:http://docs.python.org/tut/tut.html
    >
    > and next time someone points you to a specific module, have mercy and
    > *read* the doc before posting.
    >
    > As far as I'm concerned, I won't anwser any of your questions unless
    > it's obvious that you have followed these advices (but then I'll be
    > happy to help if I can).- Hide quoted text -
    >
    > - Show quoted text -


    There is 'shelve'. It's just that you have to re-update each entry
    when you modify, so it's just similarity you'd be saving.
     
    castironpi, May 16, 2008
    #7
  8. globalrev

    castironpi Guest

    On May 16, 5:19 pm, castironpi <> wrote:
    > On May 16, 4:29 pm, ""
    >
    >
    >
    >
    >
    > <> wrote:
    > > On 16 mai, 22:24, globalrev <> wrote:

    >
    > > > On 16 Maj, 21:22, jay graves <> wrote:

    >
    > > > > On May 16, 2:17 pm, globalrev <> wrote:

    >
    > > > > > i extract info from one file and put it into a dictionary.
    > > > > > i want to save that dictionary for later use, how do i do that?
    > > > > > might save a list of dictionaries or a list of classobjects too if
    > > > > > there is any difference.

    >
    > > > > use the 'pickle' module.http://docs.python.org/lib/module-pickle.html

    >
    > > > > ...
    > > > > Jay Graves

    >
    > > > pickle.dumps(mg)
    > > > pickle.load(mg)

    >
    > > > 'dict' object has no attribute 'readline'
    > > > dumps load(well i dont know but i get no complaint but running load
    > > > generates that error)

    >
    > > What about *READING THAT FUCKING MANUAL* ?

    >
    > >http://docs.python.org/lib/node316.html
    > > """
    > > dump(obj, file[, protocol])
    > >     Write a pickled representation of obj to the open file object
    > > file. This is equivalent to Pickler(file, protocol).dump(obj).

    >
    > >     If the protocol parameter is omitted, protocol 0 is used. If
    > > protocol is specified as a negative value or HIGHEST_PROTOCOL, the
    > > highest protocol version will be used.

    >
    > >     Changed in version 2.3: Introduced the protocol parameter.

    >
    > >     file must have a write() method that accepts a single string
    > > argument. It can thus be a file object opened for writing, a StringIO
    > > object, or any other custom object that meets this interface.

    >
    > > load(file)
    > >     Read a string from the open file object file and interpret it as a
    > > pickle data stream, reconstructing and returning the original object
    > > hierarchy. This is equivalent to Unpickler(file).load().

    >
    > >     file must have two methods, a read() method that takes an integer
    > > argument, and a readline() method that requires no arguments. Both
    > > methods should return a string. Thus file can be a file object opened
    > > for reading, a StringIO object, or any other custom object that meets
    > > this interface.

    >
    > >     This function automatically determines whether the data stream was
    > > written in binary mode or not.
    > > """

    >
    > > Example use:

    >
    > > >>> d = dict(a=1, b=2)
    > > >>> f = open("mydict.dat", "w")
    > > >>> pickle.dump(d, f)
    > > >>> f.close()
    > > >>> f = open("mydict.dat")
    > > >>> d2 = pickle.load(f)
    > > >>> f.close()
    > > >>> d2 == d

    > > True
    > > >>> d2

    > > {'a': 1, 'b': 2}

    >
    > > Now : it's not the first time - in a couple days - that you ask a
    > > question that you wouldn't have asked if you had taken a couple
    > > minutes doing the tutorial and/or reading the doc. This newsgroup is
    > > *very* tolerant (in most other places on usenet, you would have get a
    > > raw RTFM on the first question, and a PLONK on the second), but there
    > > are still limits, and as far as I'm concerned you're not far from
    > > them. So do yourself and the world a favour, read this:http://catb.org/~esr/faqs/smart-questions.html

    >
    > > and then this:http://docs.python.org/tut/tut.html

    >
    > > and next time someone points you to a specific module, have mercy and
    > > *read* the doc before posting.

    >
    > > As far as I'm concerned, I won't anwser any of your questions unless
    > > it's obvious that you have followed these advices (but then I'll be
    > > happy to help if I can).- Hide quoted text -

    >
    > > - Show quoted text -

    >
    > There is 'shelve'.  It's just that you have to re-update each entry
    > when you modify, so it's just similarity you'd be saving.- Hide quoted text -
    >
    > - Show quoted text -


    Now a shelf of filenames could be valuable.
     
    castironpi, May 16, 2008
    #8
  9. globalrev

    alex23 Guest

    On May 17, 7:29 am, ""
    <> wrote:
    > What about *READING THAT FUCKING MANUAL* ?


    I don't think he has the time.

    > Now : it's not the first time - in a couple days - that you ask a
    > question that you wouldn't have asked if you had taken a couple
    > minutes doing the tutorial and/or reading the doc.


    It's not just here, either. He's been posting similar low-level
    questions to groups on lisp, ruby, haskell, javascript, prolog, scheme
    and smalltalk. Either he's crowd-sourcing his learning, needs some
    serious medication for his ADD, or is just taking the piss.

    Over the past couple of months, the idiots have really taken over this
    group.

    - alex23
     
    alex23, May 17, 2008
    #9
  10. globalrev

    castironpi Guest

    On May 16, 4:23 pm, Hans Nowak <zephyrfalcon!>
    wrote:
    > globalrev wrote:
    > > pickle.dumps(mg)
    > > pickle.load(mg)

    >
    > > 'dict' object has no attribute 'readline'
    > > dumps load(well i dont know but i get no complaint but running load
    > > generates that error)

    >
    > The 'loads' and 'dumps' methods use strings:
    >
    >  >>> import pickle
    >  >>> d = {"this": 42, "that": 101, "other": 17}
    >  >>> s = pickle.dumps(d)
    >  >>> s
    > "(dp0\nS'this'\np1\nI42\nsS'other'\np2\nI17\nsS'that'\np3\nI101\ns."
    >  >>> pickle.loads(s)
    > {'this': 42, 'other': 17, 'that': 101}
    >
    > If you want to store to / restore from file, use 'dump' and 'load':
    >
    > # write to file 'out'...
    >  >>> f = open("out")
    >  >>> f = open("out", "wb")
    >  >>> pickle.dump(d, f)
    >  >>> f.close()
    >
    > # restore it later
    >  >>> g = open("out", "rb")
    >  >>> e = pickle.load(g)
    >  >>> g.close()
    >  >>> e
    > {'this': 42, 'other': 17, 'that': 101}
    >
    > Also seehttp://docs.python.org/lib/pickle-example.html.
    >
    > Hope this helps!
    >
    > --Hans


    I want to compare that cleanliness with other languages to compare
    formats.

    Is pickle.load( open( 'out', 'rb' ) ) any better or worse than
    pickle.load( 'out', 'rb' )?
     
    castironpi, May 17, 2008
    #10
  11. globalrev

    castironpi Guest

    On May 17, 3:52 am, castironpi <> wrote:
    > On May 16, 4:23 pm, Hans Nowak <zephyrfalcon!>
    > wrote:
    >
    >
    >
    >
    >
    > > globalrev wrote:
    > > > pickle.dumps(mg)
    > > > pickle.load(mg)

    >
    > > > 'dict' object has no attribute 'readline'
    > > > dumps load(well i dont know but i get no complaint but running load
    > > > generates that error)

    >
    > > The 'loads' and 'dumps' methods use strings:

    >
    > >  >>> import pickle
    > >  >>> d = {"this": 42, "that": 101, "other": 17}
    > >  >>> s = pickle.dumps(d)
    > >  >>> s
    > > "(dp0\nS'this'\np1\nI42\nsS'other'\np2\nI17\nsS'that'\np3\nI101\ns."
    > >  >>> pickle.loads(s)
    > > {'this': 42, 'other': 17, 'that': 101}

    >
    > > If you want to store to / restore from file, use 'dump' and 'load':

    >
    > > # write to file 'out'...
    > >  >>> f = open("out")
    > >  >>> f = open("out", "wb")
    > >  >>> pickle.dump(d, f)
    > >  >>> f.close()

    >
    > > # restore it later
    > >  >>> g = open("out", "rb")
    > >  >>> e = pickle.load(g)
    > >  >>> g.close()
    > >  >>> e
    > > {'this': 42, 'other': 17, 'that': 101}

    >
    > > Also seehttp://docs.python.org/lib/pickle-example.html.

    >
    > > Hope this helps!

    >
    > > --Hans

    >
    > I want to compare that cleanliness with other languages to compare
    > formats.
    >
    > Is pickle.load( open( 'out', 'rb' ) ) any better or worse than
    > pickle.load( 'out', 'rb' )?- Hide quoted text -
    >
    > - Show quoted text -


    This is a check-in on live-time writing. pickle.load didn't take two
    parameters.
     
    castironpi, May 17, 2008
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Reinhard Vornholt

    Save Viewstate in DB and restore it later

    Reinhard Vornholt, Jul 30, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    534
    Reinhard Vornholt
    Jul 30, 2003
  2. Zean Smith
    Replies:
    1
    Views:
    373
    Bruce Barker
    Nov 14, 2005
  3. Jesper Sahner
    Replies:
    5
    Views:
    445
    Galen Boyer
    Oct 28, 2004
  4. xzzy
    Replies:
    2
    Views:
    369
  5. kakarukeys
    Replies:
    4
    Views:
    638
    kakarukeys
    Oct 10, 2009
Loading...

Share This Page