multiple prints on the same line in Python

前端 未结 17 2267
独厮守ぢ
独厮守ぢ 2020-11-22 06:06

I want to run a script, which basically shows an output like this:

Installing XXX...               [DONE]

Currently, I print Installi

相关标签:
17条回答
  • 2020-11-22 06:20

    Use sys.stdout.write('Installing XXX... ') and sys.stdout.write('Done'). In this way, you have to add the new line by hand with "\n" if you want to recreate the print functionality. I think that it might be unnecessary to use curses just for this.

    0 讨论(0)
  • 2020-11-22 06:21

    None of the answers worked for me since they all paused until a new line was encountered. I wrote a simple helper:

    def print_no_newline(string):
        import sys
        sys.stdout.write(string)
        sys.stdout.flush()
    

    To test it:

    import time
    print_no_newline('hello ')
    # Simulate a long task
    time.sleep(2)
    print('world')
    

    "hello " will first print out and flush to the screen before the sleep. After that you can use standard print.

    0 讨论(0)
  • 2020-11-22 06:21

    Just in case you have pre-stored the values in an array, you can call them in the following format:

    for i in range(0,n):
           print arr[i],
    
    0 讨论(0)
  • 2020-11-22 06:22

    I found this solution, and it's working on Python 2.7

    # Working on Python 2.7 Linux
    
    import time
    import sys
    
    
    def backspace(n):
        print('\r', end='')                     # use '\r' to go back
    
    
    for i in range(101):                        # for 0 to 100
        s = str(i) + '%'                        # string for output
        sys.stdout.write(string)
        backspace(len(s))                       # back for n chars
        sys.stdout.flush()
        time.sleep(0.2)                         # sleep for 200ms
    
    0 讨论(0)
  • 2020-11-22 06:24

    You can simply use this:

    print 'something',
    ...
    print ' else',
    

    and the output will be

    something else
    

    no need to overkill by import sys. Pay attention to comma symbol at the end.

    Python 3+ print("some string", end=""); to remove the newline insert at the end. Read more by help(print);

    0 讨论(0)
  • 2020-11-22 06:25

    Here a 2.7-compatible version derived from the 3.0 version by @Vadim-Zin4uk:

    Python 2

    import time
    
    for i in range(101):                        # for 0 to 100
        s = str(i) + '%'                        # string for output
        print '{0}\r'.format(s),                # just print and flush
    
        time.sleep(0.2)
    

    For that matter, the 3.0 solution provided looks a little bloated. For example, the backspace method doesn't make use of the integer argument and could probably be done away with altogether.

    Python 3

    import time
    
    for i in range(101):                        # for 0 to 100
        s = str(i) + '%'                        # string for output
        print('{0}\r'.format(s), end='')        # just print and flush
    
        time.sleep(0.2)                         # sleep for 200ms
    

    Both have been tested and work.

    0 讨论(0)
提交回复
热议问题