How to write integers to a file?

Janko Hauser jhauser at ifm.uni-kiel.de
Fri Feb 9 08:27:52 EST 2001


Let's start with one line

# opens the file
fd=open('test.dat','w')
# create the data
data=[1,2,3,4,5,6]
# write(string) where string consists of a special format string
# later you want to create the format string once before a loop
# the parentheses are used to evaluate the thing in the right order
# then the % operator is used with a tuple.
fd.write(('%d,'* (len(data)-1) + '%d\n') % tuple(data))
fd.close()

HTH,
__Janko
-- 
  Institut fuer Meereskunde             phone: 49-431-597 3989
  Dept. Theoretical Oceanography        fax  : 49-431-565876
  Duesternbrooker Weg 20                email: jhauser at ifm.uni-kiel.de
  24105 Kiel, Germany



More information about the Python-list mailing list