Convert Dictionary to String, vice versa?

Discussion in 'Python' started by Byron, Sep 15, 2004.

  1. Byron

    Byron Guest

    Hello,

    I am a newbie and would like to know if it is possible to convert a
    string back to a dictionary?

    For example, I can convert a dictionary to a string by doing this:

    >>> names = {"Candy" : 2.95, "Popcorn" : 4.95}
    >>> strNames = str(names)
    >>> print strNames
    {'Popcorn': 4.9500000000000002, 'Candy': 2.9500000000000002}

    However, is there a way to convert a string back to a dictionary?

    Any help is very much appreciated!

    Byron
    ---
     
    Byron, Sep 15, 2004
    #1
    1. Advertising

  2. Byron wrote:
    > Hello,
    >
    > I am a newbie and would like to know if it is possible to convert a
    > string back to a dictionary?
    >
    > For example, I can convert a dictionary to a string by doing this:
    >
    > >>> names = {"Candy" : 2.95, "Popcorn" : 4.95}
    > >>> strNames = str(names)
    > >>> print strNames

    > {'Popcorn': 4.9500000000000002, 'Candy': 2.9500000000000002}
    >
    > However, is there a way to convert a string back to a dictionary?

    No way that I would know of. What is your intention?
    Does the exact format of the string representation matter? If not, take
    a look at the pickle module.
     
    Benjamin Niemann, Sep 15, 2004
    #2
    1. Advertising

  3. Byron

    Peter Hansen Guest

    Byron wrote:

    > I am a newbie and would like to know if it is possible to convert a
    > string back to a dictionary?
    >
    > For example, I can convert a dictionary to a string by doing this:
    >
    > >>> names = {"Candy" : 2.95, "Popcorn" : 4.95}
    > >>> strNames = str(names)
    > >>> print strNames

    > {'Popcorn': 4.9500000000000002, 'Candy': 2.9500000000000002}


    In this particular case, this works:

    >>> d = eval(strNames)
    >>> d

    {'Popcorn': 4.9500000000000002, 'Candy': 2.9500000000000002}

    But be cautious using "exec" and "eval()", as they are
    potential security risks -- primarily when passing them data
    that comes from a user or has been made accessible to a user,
    such as by persisting it. I suspect the answer to "how?"
    is in the FAQ. If not, it's definitely in the list archives.

    -Peter
     
    Peter Hansen, Sep 15, 2004
    #3
  4. Byron wrote:
    > I am a newbie and would like to know if it is possible to convert a
    > string back to a dictionary?
    >
    > For example, I can convert a dictionary to a string by doing this:
    >
    > >>> names = {"Candy" : 2.95, "Popcorn" : 4.95}
    > >>> strNames = str(names)
    > >>> print strNames

    > {'Popcorn': 4.9500000000000002, 'Candy': 2.9500000000000002}
    >
    > However, is there a way to convert a string back to a dictionary?


    use eval.
    >>> dictNames = eval(strNames)



    George
     
    George Yoshida, Sep 15, 2004
    #4
  5. Byron

    Byron Guest

    Hi Peter,

    Thank you for the information! This will come in quite handy...

    Byron
    ---

    Peter Hansen wrote:
    > In this particular case, this works:
    >
    > >>> d = eval(strNames)
    > >>> d

    > {'Popcorn': 4.9500000000000002, 'Candy': 2.9500000000000002}
    >
    > But be cautious using "exec" and "eval()", as they are
    > potential security risks -- primarily when passing them data
    > that comes from a user or has been made accessible to a user,
    > such as by persisting it. I suspect the answer to "how?"
    > is in the FAQ. If not, it's definitely in the list archives.
    >
    > -Peter
     
    Byron, Sep 15, 2004
    #5
  6. [Byron]:
    >
    > I am a newbie and would like to know if it is possible to convert
    > a string back to a dictionary?
    >
    > For example, I can convert a dictionary to a string by doing this:
    >
    > >>> names = {"Candy" : 2.95, "Popcorn" : 4.95}
    > >>> strNames = str(names)
    > >>> print strNames
    > {'Popcorn': 4.9500000000000002, 'Candy': 2.9500000000000002}
    >
    > However, is there a way to convert a string back to a dictionary?
    >
    > Any help is very much appreciated!


    I'm surprised no one has mentioned pickle.

    >>> import pickle
    >>> names = {"Candy" : 2.95, "Popcorn" : 4.95}
    >>> pickle.dumps(names)

    "(dp0\nS'Popcorn'\np1\nF4.9500000000000002\nsS'Candy'\np2\nF2.9500000000000002\ns."
    >>> pickle.loads(pickle.dumps(names))

    {'Popcorn': 4.9500000000000002, 'Candy': 2.9500000000000002}


    --
    Kjetil T.
     
    Kjetil Torgrim Homme, Sep 15, 2004
    #6
  7. Byron

    Byron Guest

    Hi Kjetil,

    Thank you for telling me about pickle (along with the sample of code.)
    It works great... Much appreciated!

    Byron
    ---


    Kjetil Torgrim Homme wrote:
    > [Byron]:
    >
    >> I am a newbie and would like to know if it is possible to convert
    >> a string back to a dictionary?
    >>
    >> For example, I can convert a dictionary to a string by doing this:
    >>
    >> >>> names = {"Candy" : 2.95, "Popcorn" : 4.95}
    >> >>> strNames = str(names)
    >> >>> print strNames
    >> {'Popcorn': 4.9500000000000002, 'Candy': 2.9500000000000002}
    >>
    >> However, is there a way to convert a string back to a dictionary?
    >>
    >> Any help is very much appreciated!

    >
    >
    > I'm surprised no one has mentioned pickle.
    >
    >
    >>>>import pickle
    >>>>names = {"Candy" : 2.95, "Popcorn" : 4.95}
    >>>>pickle.dumps(names)

    >
    > "(dp0\nS'Popcorn'\np1\nF4.9500000000000002\nsS'Candy'\np2\nF2.9500000000000002\ns."
    >
    >>>>pickle.loads(pickle.dumps(names))

    >
    > {'Popcorn': 4.9500000000000002, 'Candy': 2.9500000000000002}
    >
    >
     
    Byron, Sep 16, 2004
    #7
    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. -
    Replies:
    8
    Views:
    618
    Antti S. Brax
    Jun 11, 2005
  2. Flyingaway
    Replies:
    6
    Views:
    22,806
    Jack Klein
    Feb 19, 2005
  3. thinktwice
    Replies:
    4
    Views:
    903
    =?iso-8859-1?q?Kirit_S=E6lensminde?=
    Sep 7, 2006
  4. Replies:
    3
    Views:
    8,804
  5. chen li
    Replies:
    3
    Views:
    124
    Daniel Martin
    Jul 13, 2007
Loading...

Share This Page