[Tutor] updating a print statement

Kent Johnson kent37 at tds.net
Mon Dec 10 22:48:11 CET 2007


Bryan Fodness wrote:
> Here is the code.
>  
> for line in file('test.txt'):
>     the_line = line.split()
>     if the_line:
>         if the_line[0] == 'Index':
>             index = float(the_line[-1])
>             print ("\rIndex = %.3f") %index

Add a comma at the end of the above line to suppress the newline that 
print normally outputs.

Kent

> raw_input("\nExit")
>  
> Here is the output.
> 
> Index = 0.000
> Index = 0.400
> Index = 0.800
> Index = 1.000
>  
> Exit
> 
> 
> On Dec 10, 2007 4:33 PM, Kent Johnson <kent37 at tds.net 
> <mailto:kent37 at tds.net>> wrote:
> 
>     Bryan Fodness wrote:
>      > I do want to overwrite the same line.
>      >
>      > I do not see a difference between using the \r and not using it.
> 
>     How are you running the program? Try it from a command line if that is
>     not what you are doing. Can you show your new code?
> 
>     Kent
> 
>     PS Please Reply All to stay on the list.
> 
>      >
>      > On Dec 10, 2007 2:43 PM, Kent Johnson <kent37 at tds.net
>     <mailto:kent37 at tds.net>
>      > <mailto:kent37 at tds.net <mailto:kent37 at tds.net>>> wrote:
>      >
>      >     Bryan Fodness wrote:
>      >      > I have a print statement in a for loop so I can watch the
>     progress
>      >      >
>      >      > for line in file(file):
>      >      >     the_line = line.split()
>      >      >     if the_line:
>      >      >         print ("Index = %.2f") %index
>      >      >
>      >      > Is there a way that only one line will be output and the
>     variable is
>      >      > updated rather than one line for every index.
>      >
>      >     I'm not sure I understand what you are asking. index is never
>      >     updated in
>      >     the loop above.
>      >
>      >     If you want to overwrite the same line on the console, try
>      >       print "\rIndex = %.2f" % index,
>      >
>      >     note ----^
>      >          ---------------------------^
>      >
>      >     You might need some extra spaces at the end of the print to
>     'erase' a
>      >     longer previous line.
>      >
>      >     Kent
>      >
>      >
>      >
>      >
>      > --
>      > "The game of science can accurately be described as a never-ending
>      > insult to human intelligence." - João Magueijo
> 
> 
> 
> 
> -- 
> "The game of science can accurately be described as a never-ending 
> insult to human intelligence." - João Magueijo



More information about the Tutor mailing list