How to I print without newline ?

Discussion in 'Python' started by fowlertrainer@anonym.hu, May 27, 2004.

  1. Guest

    Hi !

    I want to print, but without newline. I want to create a progress for
    ftp, but the print is drop a newline for every percent.
    I want like this:

    0% 25% 50% 75% 100%

    But this happening:
    0%
    25%
    50%
    75%
    100%

    How to I prevent the newlines ?

    Thanx for help:
    FT
     
    , May 27, 2004
    #1
    1. Advertising

  2. > I want to print, but without newline. I want to create a progress for
    > ftp, but the print is drop a newline for every percent.
    > I want like this:
    >
    > 0% 25% 50% 75% 100%
    >
    > But this happening:
    > 0%
    > 25%
    > 50%
    > 75%
    > 100%
    >
    > How to I prevent the newlines ?


    Since you want spaces between the values, a comma at the end of the print
    statement will do the trick:

    >>> for p in ( 0, 25, 50, 75, 100 ):

    .... print '%s%%' % p,
    ....
    0% 25% 50% 75% 100%
    >>>


    In the more general case, where you want complete control and want to be
    able to prevent this extra space, use sys.stdout.write():

    >>> for i in xrange(20):

    .... sys.stdout.write( '+' )
    ....
    ++++++++++++++++++++>>>

    Note that there wasn't a newline at the end. You'll need to do a
    sys.stdout.write('\n') when you want one.

    -Mike
     
    Michael Geary, May 27, 2004
    #2
    1. Advertising

  3. Larry Bates Guest

    <> wrote in message
    news:...
    > Hi !
    >
    > I want to print, but without newline. I want to create a progress for
    > ftp, but the print is drop a newline for every percent.
    > I want like this:
    >
    > 0% 25% 50% 75% 100%
    >
    > But this happening:
    > 0%
    > 25%
    > 50%
    > 75%
    > 100%
    >
    > How to I prevent the newlines ?
    >
    > Thanx for help:
    > FT
    >
    >
     
    Larry Bates, May 27, 2004
    #3
  4. Larry Bates Guest

    Here's a progressbar class I wrote some time ago, hope it helps.

    -Larry

    class progressbarClass:
    def __init__(self, finalcount, progresschar=None):
    import sys
    self.finalcount=finalcount
    self.blockcount=0
    #
    # See if caller passed me a character to use on the
    # progress bar (like "*"). If not use the block
    # character that makes it look like a real progress
    # bar.
    #
    if not progresschar: self.block=chr(178)
    else: self.block=progresschar
    #
    # Get pointer to sys.stdout so I can use the write/flush
    # methods to display the progress bar.
    #
    self.f=sys.stdout
    #
    # If the final count is zero, don't start the progress gauge
    #
    if not self.finalcount : return
    self.f.write('\n------------------ %
    Progress -------------------1\n')
    self.f.write(' 1 2 3 4 5 6 7 8 9 0\n')
    self.f.write('----0----0----0----0----0----0----0----0----0----0\n')
    return

    def progress(self, count):
    #
    # Make sure I don't try to go off the end (e.g. >100%)
    #
    count=min(count, self.finalcount)
    #
    # If finalcount is zero, I'm done
    #
    if self.finalcount:
    percentcomplete=int(round(100*count/self.finalcount))
    if percentcomplete < 1: percentcomplete=1
    else:
    percentcomplete=100

    #print "percentcomplete=",percentcomplete
    blockcount=int(percentcomplete/2)
    #print "blockcount=",blockcount
    if blockcount > self.blockcount:
    for i in range(self.blockcount,blockcount):
    self.f.write(self.block)
    self.f.flush()

    if percentcomplete == 100: self.f.write("\n")
    self.blockcount=blockcount
    return

    if __name__ == "__main__":
    from time import sleep
    pb=progressbarClass(8,"*")
    count=0
    while count<9:
    count+=1
    pb.progress(count)
    sleep(0.2)

    pb=progressbarClass(100)
    pb.progress(20)
    sleep(0.2)
    pb.progress(47)
    sleep(0.2)
    pb.progress(90)
    sleep(0.2)
    pb.progress(100)
    print "testing 1:"
    pb=progressbarClass(1)
    pb.progress(1)

    <> wrote in message
    news:...
    > Hi !
    >
    > I want to print, but without newline. I want to create a progress for
    > ftp, but the print is drop a newline for every percent.
    > I want like this:
    >
    > 0% 25% 50% 75% 100%
    >
    > But this happening:
    > 0%
    > 25%
    > 50%
    > 75%
    > 100%
    >
    > How to I prevent the newlines ?
    >
    > Thanx for help:
    > FT
    >
    >
     
    Larry Bates, May 27, 2004
    #4
  5. wrote:

    > I want to print, but without newline. I want to create a progress for
    > ftp, but the print is drop a newline for every percent.
    > I want like this:
    >
    > 0% 25% 50% 75% 100%
    >
    > But this happening:
    > 0%
    > 25%
    > 50%
    > 75%
    > 100%
    >
    > How to I prevent the newlines ?


    One option:

    s = [ 0, 25, 50, 75, 100 ]
    for p in s:
    percent = "%d%%" % ( p, )
    print percent,

    Another option:

    from sys import stdout
    s = [ 0, 25, 50, 75, 100 ]
    for p in s:
    stdout.write( "%d%% " % ( p, ) )
    stdout.write( "\n" )

    --
    Chris Herborth
    Documentation Overlord, CRYPTOCard Corp. http://www.cryptocard.com/
    Never send a monster to do the work of an evil scientist.
     
    Chris Herborth, May 28, 2004
    #5
    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 Stockwell

    RE: How to I print without newline ?

    David Stockwell, May 27, 2004, in forum: Python
    Replies:
    0
    Views:
    3,472
    David Stockwell
    May 27, 2004
  2. Ray Cote
    Replies:
    1
    Views:
    327
    Michael Geary
    May 27, 2004
  3. Karlo Lozovina
    Replies:
    2
    Views:
    396
    Karlo Lozovina
    Apr 16, 2006
  4. keto
    Replies:
    0
    Views:
    964
  5. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    363
    David Cournapeau
    Dec 30, 2008
Loading...

Share This Page