Convert Dictionary to String, vice versa?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Byron

    #1

    Convert Dictionary to String, vice versa?

    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:
    [color=blue][color=green][color=darkred]
    >>> names = {"Candy" : 2.95, "Popcorn" : 4.95}
    >>> strNames = str(names)
    >>> print strNames[/color][/color][/color]
    {'Popcorn': 4.9500000000000 002, 'Candy': 2.9500000000000 002}

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

    Any help is very much appreciated!

    Byron
    ---
  • Benjamin Niemann

    #2
    Re: Convert Dictionary to String, vice versa?

    Byron wrote:[color=blue]
    > 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:
    >[color=green][color=darkred]
    > >>> names = {"Candy" : 2.95, "Popcorn" : 4.95}
    > >>> strNames = str(names)
    > >>> print strNames[/color][/color]
    > {'Popcorn': 4.9500000000000 002, 'Candy': 2.9500000000000 002}
    >
    > However, is there a way to convert a string back to a dictionary?[/color]
    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.

    Comment

    • Peter Hansen

      #3
      Re: Convert Dictionary to String, vice versa?

      Byron wrote:
      [color=blue]
      > 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:
      >[color=green][color=darkred]
      > >>> names = {"Candy" : 2.95, "Popcorn" : 4.95}
      > >>> strNames = str(names)
      > >>> print strNames[/color][/color]
      > {'Popcorn': 4.9500000000000 002, 'Candy': 2.9500000000000 002}[/color]

      In this particular case, this works:
      [color=blue][color=green][color=darkred]
      >>> d = eval(strNames)
      >>> d[/color][/color][/color]
      {'Popcorn': 4.9500000000000 002, 'Candy': 2.9500000000000 002}

      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

      Comment

      • George Yoshida

        #4
        Re: Convert Dictionary to String, vice versa?

        Byron wrote:[color=blue]
        > 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:
        >[color=green][color=darkred]
        > >>> names = {"Candy" : 2.95, "Popcorn" : 4.95}
        > >>> strNames = str(names)
        > >>> print strNames[/color][/color]
        > {'Popcorn': 4.9500000000000 002, 'Candy': 2.9500000000000 002}
        >
        > However, is there a way to convert a string back to a dictionary?[/color]

        use eval.[color=blue][color=green][color=darkred]
        >>> dictNames = eval(strNames)[/color][/color][/color]


        George

        Comment

        • Byron

          #5
          Re: Convert Dictionary to String, vice versa?

          Hi Peter,

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

          Byron
          ---

          Peter Hansen wrote:[color=blue]
          > In this particular case, this works:
          >[color=green][color=darkred]
          > >>> d = eval(strNames)
          > >>> d[/color][/color]
          > {'Popcorn': 4.9500000000000 002, 'Candy': 2.9500000000000 002}
          >
          > 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[/color]

          Comment

          • Kjetil Torgrim Homme

            #6
            Re: Convert Dictionary to String, vice versa?

            [Byron]:[color=blue]
            >
            > 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.9500000000000 002, 'Candy': 2.9500000000000 002}
            >
            > However, is there a way to convert a string back to a dictionary?
            >
            > Any help is very much appreciated![/color]

            I'm surprised no one has mentioned pickle.
            [color=blue][color=green][color=darkred]
            >>> import pickle
            >>> names = {"Candy" : 2.95, "Popcorn" : 4.95}
            >>> pickle.dumps(na mes)[/color][/color][/color]
            "(dp0\nS'Popcor n'\np1\nF4.9500 000000000002\ns S'Candy'\np2\nF 2.9500000000000 002\ns."[color=blue][color=green][color=darkred]
            >>> pickle.loads(pi ckle.dumps(name s))[/color][/color][/color]
            {'Popcorn': 4.9500000000000 002, 'Candy': 2.9500000000000 002}


            --
            Kjetil T.

            Comment

            • Byron

              #7
              Re: Convert Dictionary to String, vice versa?

              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:[color=blue]
              > [Byron]:
              >[color=green]
              >> 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.9500000000000 002, 'Candy': 2.9500000000000 002}
              >>
              >> However, is there a way to convert a string back to a dictionary?
              >>
              >> Any help is very much appreciated![/color]
              >
              >
              > I'm surprised no one has mentioned pickle.
              >
              >[color=green][color=darkred]
              >>>>import pickle
              >>>>names = {"Candy" : 2.95, "Popcorn" : 4.95}
              >>>>pickle.dump s(names)[/color][/color]
              >
              > "(dp0\nS'Popcor n'\np1\nF4.9500 000000000002\ns S'Candy'\np2\nF 2.9500000000000 002\ns."
              >[color=green][color=darkred]
              >>>>pickle.load s(pickle.dumps( names))[/color][/color]
              >
              > {'Popcorn': 4.9500000000000 002, 'Candy': 2.9500000000000 002}
              >
              >[/color]

              Comment

              Working...