typecasting: a string object to a dictionary object

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

    typecasting: a string object to a dictionary object

    Here am I,
    On the border...if someone can help me, it would be
    nice:

    I have a string:

    feature_vector. It is of the form
    <index: value, index: value, index: value>

    I want to make this string into a dictionary so that I
    can apply .keys() method

    If I apply .keys() method straight away I get this
    error:

    AttributeError: 'str' object has no attribute 'keys'

    Is there a way in which I can convert/typecast my
    string thing to dictionary ??

    Thax in advance
    Dont



    _______________ _______________ ____
    Do you Yahoo!?
    Yahoo! Mail - More reliable, more storage, less spam
    It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got your back with other convenient features like one-tap unsubscribe, free trial expiration alerts and package tracking


  • midtoad

    #2
    Re: typecasting: a string object to a dictionary object

    dont bother wrote:
    [color=blue]
    > I have a string:
    >
    > feature_vector. It is of the form
    > <index: value, index: value, index: value>
    >
    > I want to make this string into a dictionary so that I
    > can apply .keys() method[/color]

    okay, here's a solution, assuming that your < and > are part of the string.
    If not, remove the line where you take a slice. I'm sure that there are
    more Pythonic solutions, but this works...

    ---
    import string

    # define a test string
    s1 = "<name1: value1, name2: value2, name3: value3>"
    # get rid of the < and > by taking a slice
    s1 = s1[1:-1]
    # split string at the commas
    s2 = string.split(s1 ,',')
    mydict = {}
    for item in s2:
    a,b = string.split(it em,":")
    mydict[a] = b
    print mydict[a]
    print "Dictionary is: ", mydict
    ---

    cheers
    Stewart

    Comment

    • Byron Morgan

      #3
      Re: typecasting: a string object to a dictionary object

      Here is one way:

      a = {}
      feature_vector = '1:x 2:y 3:z'
      for each in feature_vector. split():
      a[each.split(':')[0]]=each.split(':' )[1]


      Byron Morgan


      dont bother <dontbotherworl d@yahoo.com> wrote in message news:<mailman.3 40.1079150274.1 9534.python-list@python.org >...[color=blue]
      > Here am I,
      > On the border...if someone can help me, it would be
      > nice:
      >
      > I have a string:
      >
      > feature_vector. It is of the form
      > <index: value, index: value, index: value>
      >
      > I want to make this string into a dictionary so that I
      > can apply .keys() method
      >
      > If I apply .keys() method straight away I get this
      > error:
      >
      > AttributeError: 'str' object has no attribute 'keys'
      >
      > Is there a way in which I can convert/typecast my
      > string thing to dictionary ??
      >
      > Thax in advance
      > Dont
      >
      >
      >
      > _______________ _______________ ____
      > Do you Yahoo!?
      > Yahoo! Mail - More reliable, more storage, less spam
      > http://mail.yahoo.com[/color]

      Comment

      • Garry Knight

        #4
        Re: typecasting: a string object to a dictionary object

        In message <Dlw4c.789400$X %5.393213@pd7tw 2no>, midtoad wrote:
        [color=blue]
        > s2 = string.split(s1 ,',')[/color]
        ....[color=blue]
        > a,b = string.split(it em,":")[/color]
        ....

        Some further processing will probably be necessary. On my system (Python 2.3
        under Mandrake Linux) your code produces the following:

        Dictionary is: {' name2': ' value2', ' name3': ' value3', 'name1': '
        value1'}

        In other words, it leaves extra spaces in all values and in all keys except
        'name1'. Putting the following lines after the a,b = string.split(it em,":")
        will cure that.

        a = a.strip()
        b = b.strip()

        This can be shortened to: a,b = a.strip(),b.str ip()
        Or, if you're not bothered about doing the 'print mydict[a]' you could
        shorten the code by ignoring the above and putting the strip() calls in the
        assignment statement:

        mydict[a.strip()] = b.strip()

        In my opinion, the first method is the best as it makes it very obvious
        what's going on.

        --
        Garry Knight
        garryknight@gmx .net ICQ 126351135
        Linux registered user 182025

        Comment

        Working...