Print in one line dynamically

后端 未结 20 3216
梦谈多话
梦谈多话 2020-11-21 23:32

I would like to make several statements that give standard output without seeing newlines in between statements.

Specifically, suppose I have:

for it         


        
相关标签:
20条回答
  • 2020-11-21 23:48

    for Python 2.7

    for x in range(0, 3):
        print x,
    

    for Python 3

    for x in range(0, 3):
        print(x, end=" ")
    
    0 讨论(0)
  • 2020-11-21 23:49

    By the way...... How to refresh it every time so it print mi in one place just change the number.

    In general, the way to do that is with terminal control codes. This is a particularly simple case, for which you only need one special character: U+000D CARRIAGE RETURN, which is written '\r' in Python (and many other languages). Here's a complete example based on your code:

    from sys import stdout
    from time import sleep
    for i in range(1,20):
        stdout.write("\r%d" % i)
        stdout.flush()
        sleep(1)
    stdout.write("\n") # move the cursor to the next line
    

    Some things about this that may be surprising:

    • The \r goes at the beginning of the string so that, while the program is running, the cursor will always be after the number. This isn't just cosmetic: some terminal emulators get very confused if you do it the other way around.
    • If you don't include the last line, then after the program terminates, your shell will print its prompt on top of the number.
    • The stdout.flush is necessary on some systems, or you won't get any output. Other systems may not require it, but it doesn't do any harm.

    If you find that this doesn't work, the first thing you should suspect is that your terminal emulator is buggy. The vttest program can help you test it.

    You could replace the stdout.write with a print statement but I prefer not to mix print with direct use of file objects.

    0 讨论(0)
  • 2020-11-21 23:52

    You can add a trailing comma to your print statement to print a space instead of a newline in each iteration:

    print item,
    

    Alternatively, if you're using Python 2.6 or later, you can use the new print function, which would allow you to specify that not even a space should come at the end of each item being printed (or allow you to specify whatever end you want):

    from __future__ import print_function
    ...
    print(item, end="")
    

    Finally, you can write directly to standard output by importing it from the sys module, which returns a file-like object:

    from sys import stdout
    ...
    stdout.write( str(item) )
    
    0 讨论(0)
  • 2020-11-21 23:54
    In [9]: print?
    Type:           builtin_function_or_method
    Base Class:     <type 'builtin_function_or_method'>
    String Form:    <built-in function print>
    Namespace:      Python builtin
    Docstring:
        print(value, ..., sep=' ', end='\n', file=sys.stdout)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file: a file-like object (stream); defaults to the current sys.stdout.
    sep:  string inserted between values, default a space.
    end:  string appended after the last value, default a newline.
    
    0 讨论(0)
  • 2020-11-21 23:54

    For those struggling as I did, I came up with the following that appears to work in both python 3.7.4 and 3.5.2.

    I expanded the range from 100 to 1,000,000 because it runs very fast and you may not see the output. This is because one side effect of setting end='\r' is that the final loop iteration clears all of the output. A longer number was needed to demonstrate that it works. This result may not be desirable in all cases, but was fine in mine, and OP didn't specify one way or another. You could potentially circumvent this with an if statement that evaluates the length of the array being iterated over, etc. The key to get it working in my case was to couple the brackets "{}" with .format(). Otherwise, it didn't work.

    Below should work as-is:

    #!/usr/bin/env python3
    
    for item in range(1,1000000):
        print("{}".format(item), end='\r', flush=True)
    
    0 讨论(0)
  • 2020-11-21 23:57

    Another answer that I'm using on 2.7 where I'm just printing out a "." every time a loop runs (to indicate to the user that things are still running) is this:

    print "\b.",
    

    It prints the "." characters without spaces between each. It looks a little better and works pretty well. The \b is a backspace character for those wondering.

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