Python Script: Print new line each time to shell r

2019-05-13 23:58发布

问题:

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

[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.

回答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 :-)



回答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:

   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'.

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

print "00000000\r1111"

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



标签: python shell