Using print instead of file.write(str)

Discussion in 'Python' started by A.M, Jun 1, 2006.

  1. A.M

    A.M Guest

    Hi,



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



    Thank you,

    Alan
    A.M, Jun 1, 2006
    #1
    1. Advertising

  2. A.M

    Jon Clements Guest

    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'
    >>> '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',
    >>> '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.



    Bruno Desthuilliers wrote:
    > 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
    Jon Clements, Jun 1, 2006
    #2
    1. Advertising

  3. A.M

    A.M Guest

    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





    "Jon Clements" <> wrote in message
    news:...
    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'
    >>> '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',
    >>> '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.



    Bruno Desthuilliers wrote:
    > 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
    A.M, Jun 1, 2006
    #3
  4. A.M

    Jon Clements Guest

    I meant 'trailing': not leading.

    mea culpa.

    Jon.

    Jon Clements wrote:
    > 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'
    > >>> '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',
    > >>> '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.
    >
    >
    >
    > Bruno Desthuilliers wrote:
    > > 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
    Jon Clements, Jun 1, 2006
    #4
  5. 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
    Bruno Desthuilliers, Jun 2, 2006
    #5
  6. A.M <> wrote:
    >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

    --
    \S -- -- http://www.chaos.org.uk/~sion/
    ___ | "Frankly I have no feelings towards penguins one way or the other"
    \X/ | -- Arthur C. Clarke
    her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
    Sion Arrowsmith, Jun 2, 2006
    #6
  7. Sion Arrowsmith a écrit :
    > A.M <> wrote:
    >
    >>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
    >



    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...)
    Bruno Desthuilliers, Jun 3, 2006
    #7
  8. A.M

    Tim Roberts Guest

    Bruno Desthuilliers <> wrote:

    >Sion Arrowsmith a écrit :
    >> A.M <> wrote:
    >>
    >>>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

    >
    >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.
    --
    - Tim Roberts,
    Providenza & Boekelheide, Inc.
    Tim Roberts, Jun 4, 2006
    #8
  9. Tim Roberts <> wrote:

    > Bruno Desthuilliers <> wrote:
    >
    > >Sion Arrowsmith a écrit :
    > >> A.M <> wrote:
    > >>
    > >>>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

    > >
    > >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.


    it's not quite impossible, just cumbersome:

    >>> for x in range(10):

    .... 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
    Alex Martelli, Jun 4, 2006
    #9
  10. A.M

    John Machin Guest

    On 3/06/2006 9:47 AM, Bruno Desthuilliers wrote:

    >
    > 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
    John Machin, Jun 4, 2006
    #10
  11. Tim Roberts a écrit :
    > Bruno Desthuilliers <> wrote:
    >
    >
    >>Sion Arrowsmith a écrit :
    >>

    (snip)
    >>>"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))

    >
    > 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 ???
    Bruno Desthuilliers, Jun 5, 2006
    #11
  12. John Machin a écrit :
    (snip)
    > ... or was that a rhetorical question?


    It was.
    Bruno Desthuilliers, Jun 5, 2006
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David
    Replies:
    2
    Views:
    470
    Thomas G. Marshall
    Aug 3, 2003
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    614
    CBFalconer
    Apr 10, 2004
  3. Sullivan WxPyQtKinter

    It is fun.the result of str.lower(str())

    Sullivan WxPyQtKinter, Mar 7, 2006, in forum: Python
    Replies:
    5
    Views:
    327
    Tim Roberts
    Mar 9, 2006
  4. Stefan Ram

    str.equals(null) or str==null ?

    Stefan Ram, Jul 31, 2006, in forum: Java
    Replies:
    21
    Views:
    14,653
    Oliver Wong
    Aug 3, 2006
  5. maestro
    Replies:
    1
    Views:
    296
    Chris
    Aug 11, 2008
Loading...

Share This Page