469,603 Members | 2,106 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,603 developers. It's quick & easy.

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
http://mail.yahoo.com

Jul 18 '05 #1
3 2297
dont bother wrote:
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


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(item,":")
mydict[a] = b
print mydict[a]
print "Dictionary is: ", mydict
---

cheers
Stewart

Jul 18 '05 #2
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 <do*************@yahoo.com> wrote in message news:<ma**************************************@pyt hon.org>...
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

Jul 18 '05 #3
In message <Dlw4c.789400$X%5.393213@pd7tw2no>, midtoad wrote:
s2 = string.split(s1,',') .... a,b = string.split(item,":")

....

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(item,":")
will cure that.

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

This can be shortened to: a,b = a.strip(),b.strip()
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
ga*********@gmx.net ICQ 126351135
Linux registered user 182025
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Kapil Khosla | last post: by
5 posts views Thread by David Rasmussen | last post: by
4 posts views Thread by vivekian | last post: by
12 posts views Thread by bwaichu | last post: by
35 posts views Thread by Abandoned | last post: by
reply views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.