How to write integers to a file?

Steve Purcell stephen_purcell at yahoo.com
Fri Feb 9 08:46:02 EST 2001


Steven Sartorius wrote:
> I'm trying to write a series of whitespace delimited integers to a file.
> The 'write' method only accepts strings and using arrays produces binary
> data when I open the file with less.  What I want is somehting like
> printf for python.  Is there any easy way to do this?  (there must
> be...this is python!)
> 

Yep:-

>>> "the number is %d" % 5
'the number is 5'
>>> 

See the docs at: http://www.python.org/doc/current/lib/typesseq-strings.html

But for your case, maybe you just want to do the following
   
>>> ints = range(5)
>>> import string
>>> string.join(map(str, ints), ' ')
'0 1 2 3 4'

   
-Steve

-- 
Steve Purcell, Pythangelist
Get testing at http://pyunit.sourceforge.net/
Available for consulting and training.
"Even snakes are afraid of snakes." -- Steven Wright




More information about the Python-list mailing list