Python overwrite new line not behaving as expected [duplicate] - python

This question already has answers here:
How to overwrite the previous print to stdout?
(18 answers)
Closed 9 years ago.
I am trying to output text to stdout, overwriting the previous text, for example
for i in range(12):
print i
but with i replacing the previous value each time rather than appearing on a new line
From looking at quite a few previous posts with similar questions it seems that there are a few ways of doing this, possibly the simplest being (for Python 3.x on)
for i in range(12):
print(i,end="\r")
sometimes with a comma at the end of the print statement, sometimes not. However, without the comma I get no output at all and with the comma I get
(None,)
(None,)
(None,)
...
Is this something related to my terminal perhaps? I get similar results no matter which of the previous posted solutions to the problem I try.
Thanks for any help!

If you do this in a terminal you won't see any output because the for loop finishes too fast for you to see the output changing. After the loop is done the terminal's prompt overwrites the output.
Try this instead:
>>> for i in range(9999999):
... print(i, end="\r")
I'm surprprised you don't get a syntax error when omitting the comma.

Related

Python print statement dependent on lines following it? [duplicate]

This question already has answers here:
Why doesn't print output show up immediately in the terminal when there is no newline at the end?
(1 answer)
Python print immediately?
(1 answer)
Closed 2 years ago.
I have the following code:
for file_name, content in corpus.items():
print('here')
content = [list(filter(lambda index: index not in remove_indices, content))]
corpus[file_name] = np.array(content).astype(np.uint32)
Where corpus is a 800,000 long dictionary with string keys and array values.
Things were taking forever so I decided to check how fast each iteration was by adding in that print statement.
If I comment the last two lines out it prints lots of heres really fast, so there's no problem with my iterator. What's really weird is that when I uncomment the last two lines, here takes a long time to print, even for the first one! It's like the print statement is somehow aware of the lines that follow it.
I guess my question speaks for itself. I'm in Jupyter notebook, if that helps.

Overwriting printed statements in the IDLE in Python [duplicate]

This question already has answers here:
How to output to the same line overwriting the previous line?
(4 answers)
Closed 3 years ago.
I am looking for a method so that when something is printed continuously, it overwrite what the previous printed statement says. for example, if I am making a counter, normally the outcome in the IDLE would be: 1 2 3 4...., but however I'm looking to rewrite/overwrite what the previous printed statement says so it say "1" for a second then "2" appears but we can no longer see "1". Any suggestions? Sorry about how wordy this question is, I was having a hard time trying to write this where another person understands.
import time
arr = [1,2,3,4]
for element in arr:
print(element, end='\r')
time.sleep(1)
end='\r' in the print statement does the trick

How to read a line in the terminal with python [duplicate]

This question already has answers here:
Capture stdout from a script?
(11 answers)
Closed 3 years ago.
I'm trying to get a already written line in python 3, but I haven't found any function that can read a line from the terminal. It should work something like sys.stdout.read(), or sys.stdout.readline() but this function just throws an error.
If you mean to read from the user/a pipe, then simply use input.
However, from your comments it seems like you want to be able to read from what has already been printed.
To do this, you have a few options. If you don't actually want it to display on the terminal, and you only care about certain part of the output, then you can use contextlib.redirect_stdout and contextlib.redirect_stderr. You can combine this with io.StringIO to capture the output of your application to a string. This has been discussed in the question Capture stdout from a script in Python
However, if you want to have something which provides you both a means of printing to the terminal and giving you the lines, then you will need to implement your own type which inherits from io.TextIOBase or uses io.TextIOWrapper.
Do you mean something like this?
name = input("Enter a name: ")
print(name)

Why semi-colon in python after end of print and variable working? [duplicate]

This question already has answers here:
What does a semicolon do?
(5 answers)
Closed 5 years ago.
I am new to python and currently working in it.
I have write one simple program, as i read somewhere that semicolon not allowed or required in python to end statement. but i have use that and till its working fine! anyone explain me
why its possible?
here is code.
a = 10;
if a == 10:
print "value of a is %s"%(a);
else:
print "value of a is not %s"%(a);
semicolon is allowed as statement separator
>>> a=1;b=2;c=3
>>> print(a,b,c)
1 2 3
It is not required if you write each statement in a new line
Python does not require semi-colons to terminate statements. Semi-colons can be used to delimit statements if you wish to put multiple statements on the same line.

Python Gives me empty output [duplicate]

This question already has answers here:
Running python script in terminal, nothing prints or shows up - why?
(2 answers)
Closed 2 years ago.
At first I thought PythonScriptor was the problem, but I tried print('Hello World') and the output was fine.When ever I copy and paste a code it just gives me an empty line. I am currently learning Subroutines in Python, but I completely don't get it.
def myFirstSubroutine():
for i in range(1,3):
print('This is a subroutine')
The code snippet you just showed us defined the function. To execute the function, now you need to call it.
>>> myFirstSubroutine()
This is a subroutine
This is a subroutine
Thanks CoryKramer for his right answer. You need to call the function myFirstSubroutine() to get the output.
Pay attention to that lowercase letter variable is not equal to capital letter variable in Python. That means you need to use exact the function name myFirstSubroutine().
As well as 'I copied what you gave me , it doesn't work.', it can work if you really copy it instead of spelling the 'myfirstsubroutine' by yourself.

Categories