Using print instead of file.write(str)

A

A.M

Hi,



I found print much more flexible that write method. Can I use print instead
of file.write method?



Thank you,

Alan
 
J

Jon Clements

Didn't know of the >> syntax: lovely to know about it Bruno - thank
you.

To the OP - I find the print statement useful for something like:
print 'this','is','a','test'(with implicit newline and implicit spacing between parameters)

If you want more control (more flexibility, perhaps?) over the
formatting of the output: be it spacing between parameters or newline
control, use the methods Bruno describes below.

I'm not sure if you can suppress the spacing between elements (would
love to be corrected though); to stop the implicit newline use
something like
print 'testing',(but - with the leading comma, the newline is suppressed)

I personally find that print is convenient for sentences (or writing
'lines').

Thought it worth pointing this out in case, like some I know, you come
across a cropper with certain output streams.

All the best,

Jon.
 
A

A.M

Yes, it saved my time big time.

Thank you Bruno.



I use the print >>>file to generate HTML files. print is very flexible and
nice.



The dictionary formatting that Brunto said is awesome!



Thanks again,

Alan





Didn't know of the >> syntax: lovely to know about it Bruno - thank
you.

To the OP - I find the print statement useful for something like:
print 'this','is','a','test'(with implicit newline and implicit spacing between parameters)

If you want more control (more flexibility, perhaps?) over the
formatting of the output: be it spacing between parameters or newline
control, use the methods Bruno describes below.

I'm not sure if you can suppress the spacing between elements (would
love to be corrected though); to stop the implicit newline use
something like
print 'testing',(but - with the leading comma, the newline is suppressed)

I personally find that print is convenient for sentences (or writing
'lines').

Thought it worth pointing this out in case, like some I know, you come
across a cropper with certain output streams.

All the best,

Jon.
 
B

Bruno Desthuilliers

A.M a écrit :
Hi,


I found print much more flexible that write method. Can I use print instead
of file.write method?

f = open("/path/to/file")
print >> f, "this is my %s message" % "first"
f.close()

To print to stderr:

import sys
print >> sys.stderr, "oops"

FWIW, you and use string formating anywhere, not only in print statements:

s = "some %s and % formating" % ("nice", "cool")
print s

You can also use "dict formating":

names = {"other": "A.M.", "me" : "bruno"}
s = "hello %(other)s, my name is %(me)s" % names
 
S

Sion Arrowsmith

A.M said:
I found print much more flexible that write method.

"more flexible"? More convenient, yes. More powerful, maybe. But I
don't see more flexible. Everything print can to stdout.write() can
do. The reverse isn't true. eg (this appears to be a FAQ on this
group, although I can't find it in the FAQ):

for x in range(10):
sys.stdout.write(str(x))

to print:

0123456789
 
B

Bruno Desthuilliers

Sion Arrowsmith a écrit :
"more flexible"? More convenient, yes. More powerful, maybe. But I
don't see more flexible. Everything print can to stdout.write() can
do. The reverse isn't true. eg (this appears to be a FAQ on this
group, although I can't find it in the FAQ):

for x in range(10):
sys.stdout.write(str(x))

to print:

0123456789


The reverse isn't true ???

print "".join(str(x) for x in range(10))

(which BTW should be faster, since it's only one I/O, instead of ten
with your version.)

Now, given:

bird = "parrot"
beautiful = "dead"

How would you do the following with f.write() ?

print "this", bird, "is", beautiful

(without using string formating, of course...)
 
T

Tim Roberts

Bruno Desthuilliers said:
Sion Arrowsmith a écrit :

The reverse isn't true ???

print "".join(str(x) for x in range(10))

What he meant it that it is impossible to produce "0123456789" using 10
separate print statements, while it IS possible with 10 separate writes.
 
A

Alex Martelli

Tim Roberts said:
What he meant it that it is impossible to produce "0123456789" using 10
separate print statements, while it IS possible with 10 separate writes.

it's not quite impossible, just cumbersome:
.... print x,
.... sys.stdout.softspace=0
....
0123456789>>>

Yes, you do need the softspace assignments -- but then, in the write
version you need the explicit str calls, so it's not as if in either
case you're using "just" the print or write-call.

The differences in terms of convenience are surely there (in different
circumstances they will favor one or the other of the two approaches),
but I don't see such differences in either flexibility or power (if one
ignores the issue of convenience, the same tasks can be performed with
either approach).


Alex
 
J

John Machin

Now, given:

bird = "parrot"
beautiful = "dead"

How would you do the following with f.write() ?

print "this", bird, "is", beautiful

(without using string formating, of course...)

Like this:

f.write((' '.join(str(x) for x in ['this', bird, 'is', beautiful]) + '\n'))

.... or was that a rhetorical question?

Cheers,
John
 
B

Bruno Desthuilliers

Tim Roberts a écrit :
What he meant it that it is impossible to produce "0123456789" using 10
separate print statements, while it IS possible with 10 separate writes.

why on earth would someone try to use 10 consecutive I/O operations on
the same stream when it can be done with 1 ???
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top