determining type

Quinn Dunkan quinn at baht.ugcs.caltech.edu
Wed Mar 22 16:15:31 EST 2000


On 22 Mar 2000 12:10:24 -0500, Alex <alex at somewhere.round.here> wrote:
>
>> I'm trying to write a function to format the dictionary so that when I
>> print it, rather than looking like {'foo' : 'bar', 'who' : 'what'}, it
>> looks like
>> 
>> {
>> 'foo' : 'bar',
>> 'who' : 'what',
>> }
>
>import string
>
>def dictionary_str (d):
>    output = ['{\n']
>    for pair in d.items ():
>        output.append ('%s : %s,\n' % tuple (map (repr, pair)))
>    output.append ('}')
>    return string.join (output, '')
>
>print dictionary_str ({'foo' : 'bar', 'who' : 'what'})

or you could do

import pprint
pprint.pprint(dict)

Which won't format literally how you wanted, but maybe you're not too fussy
about that (or could subclass PrettyPrinter).



More information about the Python-list mailing list