By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,782 Members | 1,154 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,782 IT Pros & Developers. It's quick & easy.

String formatting with dictionaries

P: n/a
Consider the following simple dictionary
e={1:'one', 2: 'two'}
e[1]
'one'


However, If I attempt to print e[1] using a formatted string
print " %(1)s" %e,

I get a KeyError: '1'

Clearly Python is converting the number 1 to the string '1' before
looking it up in the dictionary. Furthermore, this seems to happen
only when creating formatted strings: the dictionary can be directly
accessed as shown above. How can I modify my formatted string
statement to correctly access the dictionary.

I am aware that I can make it work by changing e to
e={'1':'one', '2': 'two'}
but I do want to find out

a) what is needed make it work in its current form, and
b) why it does not work in the seemingly obvious way I have written it
above

Sincerely
Thomas Philips
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
tk****@hotmail.com (Thomas Philips) wrote in
news:b4**************************@posting.google.c om:
Clearly Python is converting the number 1 to the string '1' before
looking it up in the dictionary. Furthermore, this seems to happen
only when creating formatted strings: the dictionary can be directly
accessed as shown above. How can I modify my formatted string
statement to correctly access the dictionary.


The mapping key is a sequence of characters, i.e. it already is a string.
Python is not converting it to a string, it is simply not converting it
into a number.

To make it work, you could subclass dict so that strings get converted to
integers automatically on lookup, or just convert the keys to strings:

print " %(1)s" % dict([(str(k),e[k]) for k in e])

Jul 18 '05 #2

P: n/a
tk****@hotmail.com (Thomas Philips) writes:
I am aware that I can make it work by changing e to
e={'1':'one', '2': 'two'}
but I do want to find out

a) what is needed make it work in its current form, and
What you just did above - use strings as keys in your dictionary.
b) why it does not work in the seemingly obvious way I have written it
above


Perhaps probably because your intuition isn't firmly grounded in Dutch
sensibilities? Or I could just say "because that's not how the string
formatting operator works" but that doesn't help much does it :-)

If we come at it from your "obvious" comment, one could ask why you
think it treating it as a number is obvious? When you write:

"blah blah blah %(name)s blah blah" % some_dict

The formatting operator upon finding "%(name)s", looks up "name" in
the dictionary and then applies the "s" (string) format to it. If you
think about it the formatting operator just has access to your format
string as, in fact, a string. And in the specific case of dictionary
keys, the element in between the () is a label to be used (a sequence
of characters as per 2.3.6.2 in the library reference, and both there
and 7.1 in the tutorial show examples using string keys).

Now, there's no additional markup in the format string to indicate
that the dictionary key is anything other than a sequence of
characters, and since in general Python tries to avoid ever guessing
at what you mean, it just uses those characters directly.

In other words, you may see the "1" in the format string as a number,
but you've entered it into your program as part of a string, and
that's how Python (and the string formatting operator) see it. Maybe
not what you desired, but it's how the formatting operator works in
this case.

-- David

Jul 18 '05 #3

P: n/a
In article <b4**************************@posting.google.com >,
tk****@hotmail.com (Thomas Philips) wrote:
Consider the following simple dictionary
e={1:'one', 2: 'two'}
e[1]
'one'
However, If I attempt to print e[1] using a formatted string
print " %(1)s" %e,

I get a KeyError: '1'

Clearly Python is converting the number 1 to the string '1' before
looking it up in the dictionary.
Not clear at all. Any immutable value can be a
dictionary key.

The mapping key is a sequence of characters in a format
string.. a slice of a string, you might say, and it will
only match a key that is a string.

Furthermore, this seems to happen
only when creating formatted strings: the dictionary can be directly
accessed as shown above. How can I modify my formatted string
statement to correctly access the dictionary.

I am aware that I can make it work by changing e to
e={'1':'one', '2': 'two'}
but I do want to find out

a) what is needed make it work in its current form, and
b) why it does not work in the seemingly obvious way I have written it
above


It wouldn't be enough the make %(1)s retrive 'one' in the
dictionary e; you'd actually be interpreting Python
expressions inside the format string, and people would
require %(7-5)s to come out as two, just like e[7-5] did.
f={'7-5':'three'}
print "%(7-5)s" % f

three
Regards. Mel.
Jul 18 '05 #4

P: n/a
Thomas> I am aware that I can make it work by changing e to
Thomas> e={'1':'one', '2': 'two'}
Thomas> but I do want to find out

Thomas> a) what is needed make it work in its current form, and

How about subclassing dict so that __getitem__ tries calling int() on the
key it's presented if it fails to find the key?

class mydict(dict):
### untested! ###
def __getitem__(self, key):
try:
return dict.__getitem__(self, key)
except KeyError:
try:
key = int(key)
except ValueError:
raise
else:
return dict.__getitem__(self, key)

Thomas> b) why it does not work in the seemingly obvious way I have
Thomas> written it above

Because strings are not ints.

Skip

Jul 18 '05 #5

P: n/a
David Bolen wrote:
b) why it does not work in the seemingly obvious way I have written it
above

Perhaps probably because your intuition isn't firmly grounded in Dutch
sensibilities? Or I could just say "because that's not how the string
formatting operator works" but that doesn't help much does it :-)


Funny, but when I read Thomas' original post, I thought
what he expected was blindingly obvious and
intuitive... but once it was pointed out that the keys
were being taken from a string, it became blindingly
obvious and intuitive that they had to be strings
themselves!

It's a funny old world, inn't?
--
Steven
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.