convert dictionary to string

Giles Brown giles_brown at hotmail.com
Thu Apr 10 07:47:28 EDT 2003


matthew <matthew at newsgroups.com> wrote in message news:<b72a7l$ifj$1 at lust.ihug.co.nz>...
> hi,
> 
> can anyone tell what is the fastest way to convert a dictionary to a string?
> 
> eg: d={'A':45.10, 'F':56.50,...}
> to 'A45.10F56.50...'
> 
> Thanks very much. matthew

Heres one option using the [c]StringIO module:

Python 2.2.1 (#34, Apr  9 2002, 19:34:33) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> d={'A':45.10, 'F':56.50,}
>>> from cStringIO import StringIO
>>> sio = StringIO()
>>> for item in d.iteritems():
	sio.write('%s%.2f' % item)

	
>>> sio.getvalue()
'A45.10F56.50'
>>>




More information about the Python-list mailing list