Problem with The Formatting Operator

Fred Gansevles (gansevle@cs.utwente.nl)
8 Feb 1995 12:41:55 GMT

Hi
when I try to print a string containing literal '%' _and_
variable-references-by-name I get stuck.

This is what I've tried sofar:

err1.py:
import sys
somestring = "something %%and some other %(var)s"

dict = { 'key_1': { 'var': '<some_val>' },
'key_2': { 'var': '<some_other_val>'}
}

print somestring % dict[sys.argv[1]]

$ python err1.py key_1
Traceback (innermost last):import sys
File "err1.py", line 8, in ?
print somestring % dict[sys.argv[1]]
TypeError: not all arguments converted

err2.py:
import sys
somestring = "something \%and some other %(var)s"

dict = { 'key_1': { 'var': '<some_val>' },
'key_2': { 'var': '<some_other_val>'}
}

print somestring % dict[sys.argv[1]]
$ python err2.py key_2
Traceback (innermost last):
File "err2.py", line 8, in ?
print somestring % dict[sys.argv[1]]
ValueError: unsupported format character

I have a solution, but that is not what i really want. My
solution is:
sol.py:
import sys
somestring = "something %(percent)cand some other %(var)s"

dict = { 'key_1': { 'var': '<some_val>', 'percent': '%' },
'key_2': { 'var': '<some_other_val>', 'percent': '%' }
}

print somestring % dict[sys.argv[1]]
$ python sol.py key_2
something %and some other <some_other_val>

Has anybody a (better) solution to my problem ?

Please reply by mail since I don't read this group often.
____________________________________________________________________________
Fred Gansevles <gansevle@cs.utwente.nl> Phone: +31 53 89 3723
Org.: Twente University, Fac. of CS, Box 217, 7500 AE Enschede, Netherlands
"I Do not seek, I find" - Pablo Picasso