[Tutor] file operations and formatting

andy surany mongo57a@comcast.net
Mon Dec 16 18:59:02 2002


Thanks! I'll give it a try.

I knew that I was close.... I had tried '%4.2f %s' % (file.write (a,b=
))

Regards,

Andy

-----Original Message-----
=46rom: Gon=E7alo Rodrigues <op73418@mail.telepac.pt>
To: tutor@python.org <tutor@python.org>
Date: Monday, December 16, 2002 6:48 PM
Subject: Re: [Tutor] file operations and formatting


>
>----- Original Message -----
>From: "Scott Widney" <SWidney@ci.las-vegas.nv.us>
>To: "'andy surany'" <mongo57a@comcast.net>; <tutor@python.org>
>Sent: Monday, December 16, 2002 11:38 PM
>Subject: RE: [Tutor] file operations and formatting
>
>
>> > a=3D99.99 # a float
>> > b=3D'hello world'
>> > file=3Dopen('junk.txt','w')
>> >
>> > What I want is to write a and b to junk.txt with a space in
>> > between (I'm using file.write).
>>
>> ### Here's the short answer
>> >>> a =3D 99.99
>> >>> b =3D 'hello world'
>> >>> fd =3D file('junk.txt', 'w')
>> >>> fd.write("%f %s" % a, b)
>>
>
>You will get an error here since % has lower precedence than , so th=
at
>Python parses this as ("%f %s" % a), b. It should be
>
>fd.write("%f %s" % (a, b))
>
>> > I tried using a format to write, but that didn't work -
>> > mostly because I think that I don't understand formatting
>> > as well as I should. So does anyone have a good
>> > reference/tutorial for formatting?
>>
>> You'll find the options and descriptions to use in formatting
statements
>in
>> the Python Library Reference, section 2.2.6.2 String Formatting
>Operations.
>> Here's a shortcut:
>>
>> http://www.python.org/doc/current/lib/typesseq-strings.html#l2h-14=
8
>>
>>
>> Enjoy!
>> Scott
>>
>
>All the best,
>G. Rodrigues
>
>
>_______________________________________________
>Tutor maillist  -  Tutor@python.org
>http://mail.python.org/mailman/listinfo/tutor