String formatting with dictionaries

T

Thomas Philips

Consider the following simple dictionary
e={1:'one', 2: 'two'}
e[1]
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
 
M

Mel Wilson

Consider the following simple dictionary
e={1:'one', 2: 'two'}
e[1]
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.
three


Regards. Mel.
 
S

Skip Montanaro

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
 
D

Duncan Booth

(e-mail address removed) (Thomas Philips) wrote in
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])
 
D

David Bolen

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
 
S

Steve

David said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,278
Latest member
BuzzDefenderpro

Latest Threads

Top