How to convert a String to a Dictionary?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • neeru29
    New Member
    • Feb 2009
    • 21

    How to convert a String to a Dictionary?

    I have new to python programming.

    I have sucessfully converted a dictionary into a string.
    but i want to know how can convert the string back to the dictionary.

    let us say that 'd' is dictionary

    >>>from cStringIO import StringIO
    >>> d={'a':'google' , 'b':80, 'c':'string'}
    >>> sio = StringIO()
    >>> for item in d.iteritems():
    ... sio.write('%s, %s / ' %item)

    >>> sio.getvalue()
    a, google / c, string / b, 80

    i have stored this value into a variable.

    i want convert back this string into dictionary. how can i do this..

    Plz help me out with this........... .
    thxs in advance........
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    You can accomplish this by parsing the string.
    Code:
    items = [s for s in sio.getvalue().split(' / ') if s]
    dd = {}
    for item in items:
        key,value = item.split(', ')
        dd[key] = value
    
    print dd
    Output:
    Code:
    >>> {'a': 'google', 'c': 'string', 'b': '80'}
    >>>

    Comment

    • neeru29
      New Member
      • Feb 2009
      • 21

      #3
      thanks for the help dude....

      Comment

      • sharat87
        New Member
        • Feb 2009
        • 6

        #4
        saving?

        if you are saving the dictionary for later use (writing the string to a file and later reading it and reconstructing the dictionary...)

        you may want to look at a serialization pattern like the pickling library.

        regards,
        sharat87

        Comment

        • neeru29
          New Member
          • Feb 2009
          • 21

          #5
          thxs,..

          But i'm trying to store the dictionary to database, but the database doesn't support to store the dictionary. as i had to convert the dictionary to string...

          Comment

          • sharat87
            New Member
            • Feb 2009
            • 6

            #6
            if that's what you want, I suggest you take at look at how you can use the pickling library... you do something like this...

            import pickle as pk
            d={'a':'google' , 'b':80, 'c':'string'}
            d_str = pk.dumps(d)
            # put the string d_str into the database...
            # get the string from the database and do
            d = pk.loads(d_str)
            # and there you have your dictionary back

            i did not test the above code.. but it will be something like that..
            in my opinion, it will be much easier for you to use pickling... but if you are going to use those strings for humans to read.. then forget it! :)

            Comment

            • neeru29
              New Member
              • Feb 2009
              • 21

              #7
              no, these values are not used for the human reading........ ..
              By using the pickling can I store the dictionary into the database......

              Comment

              • sharat87
                New Member
                • Feb 2009
                • 6

                #8
                yes, in the example I showed you in my previous post... you just store the d_str string into the database... that's the string you will ever need.

                using dumps(d, protocol=1) will give smaller strings, but will have binary data... i am not sure if those can be written to a databse.. but the code in my above post should work just fine

                Note: i did not test it.. so if u have any more problems, just drop in here :)

                Comment

                Working...