Python脚本:每次打印新行到shell而不是更新现有行

时间:2022-01-26 13:53:05

I am a noob when it comes to python. I have a python script which gives me output like this:

说到python,我是一个菜鸟。我有一个python脚本,它给我这样的输出:

[last] ZVZX-W3vo9I: Downloading video webpage
[last] ZVZX-W3vo9I: Extracting video information
[download] Destination: myvideo.flv
[download]   9.9% of 10.09M at    3.30M/s ETA 00:02

The last line keeps getting updated with new values of progress. I want to change this. Instead of updating I want a new line to be printed each time. How can i do this? I think the part concerned is this bit:

最后一行不断更新进度的新值。我想改变这个。而不是更新我想要每次打印一个新行。我怎样才能做到这一点?我认为有关的部分是这样的:

def report_progress(self, percent_str, data_len_str, speed_str, eta_str):
    """Report download progress."""
    self.to_stdout(u'\r[download] %s of %s at %s ETA %s' %
        (percent_str, data_len_str, speed_str, eta_str), skip_eol=True)

If more code needs to be seen please let me know so that I can show you what is needed to solve this.

如果需要查看更多代码,请告诉我,以便我可以向您展示解决此问题所需的内容。

Thank you very much for any help.

非常感谢您的帮助。

4 个解决方案

#1


If I understand your request properly, you should be able to change that function to this:

如果我理解您的请求,您应该能够将此功能更改为:

def report_progress(self, percent_str, data_len_str, speed_str, eta_str):
    """Report download progress."""
    print u'[download] %s of %s at %s ETA %s' % (percent_str, data_len_str, speed_str, eta_str)

That will print the output on a new line each time.

这将每次在新行上打印输出。

#2


I'm thinking you may just need to change:

我想你可能只需要改变:

skip_eol=True

to:

skip_eol=False

and get rid of the "\r" to see what happens. I think you'll be pleasantly surprised :-)

并摆脱“\ r \ n”看看会发生什么。我想你会惊喜的:-)

#3


You can take out the \r, which moves to the cursor back to the beginning of the line and take out the skip_eol=True probably. Perhaps:

你可以取出\ r,它移动到光标回到行的开头并取出skip_eol = True。也许:

   self.to_stdout(u'[download] %s of %s at %s ETA %s' %
        (percent_str, data_len_str, speed_str, eta_str))

#4


The "update" effect is achieved by '\r'.

“更新”效果由'\ r'实现。

Try this in a Python (2.x) shell:

在Python(2.x)shell中尝试这个:

print "00000000\r1111"

\r just returns the cursor to the beginning of the line.

\ r只是将光标返回到行的开头。

#1


If I understand your request properly, you should be able to change that function to this:

如果我理解您的请求,您应该能够将此功能更改为:

def report_progress(self, percent_str, data_len_str, speed_str, eta_str):
    """Report download progress."""
    print u'[download] %s of %s at %s ETA %s' % (percent_str, data_len_str, speed_str, eta_str)

That will print the output on a new line each time.

这将每次在新行上打印输出。

#2


I'm thinking you may just need to change:

我想你可能只需要改变:

skip_eol=True

to:

skip_eol=False

and get rid of the "\r" to see what happens. I think you'll be pleasantly surprised :-)

并摆脱“\ r \ n”看看会发生什么。我想你会惊喜的:-)

#3


You can take out the \r, which moves to the cursor back to the beginning of the line and take out the skip_eol=True probably. Perhaps:

你可以取出\ r,它移动到光标回到行的开头并取出skip_eol = True。也许:

   self.to_stdout(u'[download] %s of %s at %s ETA %s' %
        (percent_str, data_len_str, speed_str, eta_str))

#4


The "update" effect is achieved by '\r'.

“更新”效果由'\ r'实现。

Try this in a Python (2.x) shell:

在Python(2.x)shell中尝试这个:

print "00000000\r1111"

\r just returns the cursor to the beginning of the line.

\ r只是将光标返回到行的开头。