如何使用\ r在同一行上打印? [重复]

时间:2022-06-11 00:05:15

This question already has an answer here:

这个问题在这里已有答案:

I have made a downloader:

我做了一个下载器:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

from __future__ import print_function, division, absolute_import, unicode_literals
import os
import argparse
try:
    from urllib2 import urlopen
except ImportError:
    from urllib.request import urlopen

# {{ Argument parser
parser = argparse.ArgumentParser(
    prog='downloader',
    description='a featureful downloader'
)

parser.add_argument(
    '-o',
    dest='outputfile'
)

parser.add_argument(
    '-r',
    dest='remotefile'
)

downloader = parser.parse_args()
# }}

# {{ default local filename
if downloader.outputfile:
    default_output_file = downloader.outputfile
else:
    default_output_file = os.path.split(downloader.remotefile)[-1]
# }}

u = urlopen(downloader.remotefile)
meta = u.info()
file_size  = int(dict(meta.items())['Content-Length'])
print("Downloading: %s Bytes: %s" % (default_output_file, file_size))

with open(default_output_file, 'wb') as f:
    file_size_dl = 0
    block_sz = 8192
    while True:
        buffer = u.read(block_sz)
        if not buffer:
            break

        file_size_dl += len(buffer)
        f.write(buffer)
        status = r"%10d  [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
        status = status + chr(8)*(len(status)+1)
        print(status)

A sample run of this script would be:

此脚本的示例运行将是:

python3 downloader.py -o ubuntu.iso -r http://releases.ubuntu.com/13.04/ubuntu-13.04-desktop-i386.iso

And a sample output will be:

并且示例输出将是:

Downloading: ubuntu.iso Bytes: 832569344
      8192  [0.00%]
     16384  [0.00%]
     24576  [0.00%]
     32768  [0.00%]
     40960  [0.00%]
     49152  [0.01%]
     57344  [0.01%]
     65536  [0.01%]
     73728  [0.01%]
     81920  [0.01%]
     90112  [0.01%]
     98304  [0.01%]
    106496  [0.01%]
    114688  [0.01%]
    122880  [0.01%]
    131072  [0.02%]
    139264  [0.02%]
    147456  [0.02%]
    155648  [0.02%]
    163840  [0.02%]

Note that as the loop goes the output is printed on a separate lines. I know I can use \r (carriage return) to do that. But I have confusion where and how to use that.

请注意,随着循环的进行,输出将打印在单独的行上。我知道我可以使用\ r(回车)来做到这一点。但我有困惑在哪里以及如何使用它。

3 个解决方案

#1


12  

Put the \r at the beginning or end of your printed string, e.g. '\rthis string will start at the beginning of the line'. or 'the next string will start at the beginning of the line\r'. Conceptually, \r moves the cursor to the beginning of the line and then keeps outputting characters as normal.

将\ r放在打印字符串的开头或结尾处,例如'\ rthis字符串将从该行的开头'开始。或'下一个字符串将从行开头\ r'开始。从概念上讲,\ r \ n将光标移动到行的开头,然后保持正常输出字符。

You also need to tell print not to automatically put a newline character at the end of the string. In python3, you can use end="" as in this previous * answer.

您还需要告诉print不要在字符串的末尾自动添加换行符。在python3中,您可以使用end =“”,就像之前的*答案一样。

Alternatively, instead of using print you can do import sys and sys.stdout.write('whatever'), which sends only the exact characters to stdout without an implicit newline. You'll probably also want to use sys.stdout.flush(), without which it will store characters in a buffer rather than printing them immediately.

或者,您可以执行导入sys和sys.stdout.write('whatever'),而不是使用print,它只将精确的字符发送到stdout而不使用隐式换行符。您可能还想使用sys.stdout.flush(),如果没有它,它会将字符存储在缓冲区中而不是立即打印它们。

#2


3  

Add an , end="\r" to you print function. Also make sure your printed data has enough space to overwrite the previous printed data or be of the same length since just moving to the same line would not automatically clear the previous contents.

在打印功能中添加一个,end =“\ r”。还要确保您的打印数据有足够的空间来覆盖以前打印的数据或具有相同的长度,因为只要移动到同一行就不会自动清除以前的内容。

#3


2  

You can use sys.stdout.write:

您可以使用sys.stdout.write:

import time
import sys

def timer(t=5):
    t0 = time.time()
    now = t0
    while now-t0 < t:
        now = time.time()
        timestr = '\r%%%i\t' %(100*(now-t0)/t)
        sys.stdout.write(timestr)
        sys.stdout.flush()
        time.sleep(0.1)

#1


12  

Put the \r at the beginning or end of your printed string, e.g. '\rthis string will start at the beginning of the line'. or 'the next string will start at the beginning of the line\r'. Conceptually, \r moves the cursor to the beginning of the line and then keeps outputting characters as normal.

将\ r放在打印字符串的开头或结尾处,例如'\ rthis字符串将从该行的开头'开始。或'下一个字符串将从行开头\ r'开始。从概念上讲,\ r \ n将光标移动到行的开头,然后保持正常输出字符。

You also need to tell print not to automatically put a newline character at the end of the string. In python3, you can use end="" as in this previous * answer.

您还需要告诉print不要在字符串的末尾自动添加换行符。在python3中,您可以使用end =“”,就像之前的*答案一样。

Alternatively, instead of using print you can do import sys and sys.stdout.write('whatever'), which sends only the exact characters to stdout without an implicit newline. You'll probably also want to use sys.stdout.flush(), without which it will store characters in a buffer rather than printing them immediately.

或者,您可以执行导入sys和sys.stdout.write('whatever'),而不是使用print,它只将精确的字符发送到stdout而不使用隐式换行符。您可能还想使用sys.stdout.flush(),如果没有它,它会将字符存储在缓冲区中而不是立即打印它们。

#2


3  

Add an , end="\r" to you print function. Also make sure your printed data has enough space to overwrite the previous printed data or be of the same length since just moving to the same line would not automatically clear the previous contents.

在打印功能中添加一个,end =“\ r”。还要确保您的打印数据有足够的空间来覆盖以前打印的数据或具有相同的长度,因为只要移动到同一行就不会自动清除以前的内容。

#3


2  

You can use sys.stdout.write:

您可以使用sys.stdout.write:

import time
import sys

def timer(t=5):
    t0 = time.time()
    now = t0
    while now-t0 < t:
        now = time.time()
        timestr = '\r%%%i\t' %(100*(now-t0)/t)
        sys.stdout.write(timestr)
        sys.stdout.flush()
        time.sleep(0.1)