adrian5750
Posts: 27
Joined: Mon Mar 06, 2017 10:10 pm

RPi / python print format

Mon Mar 06, 2017 10:20 pm

HI
I'm new to all this! - particularly python.

I'm trying to set up a Raspberry Pi to read from a thermocouple via a MAX 31855 interface card.
Actually - that bit is working.
What I'd like to do is have the temperature displayed, using a print statement, but overwriting the same line each time...

Currrently - the printout looks like
Temperature = 22*c Temperature=22*c Temperature = 22*c Temperature=22*c Temperature = 22*c Temperature=22*c

But I'd like it to just print out in the same position on the screen - like this
Temperature = 22*c
where the 22*c changes as the temperature changes.

Currently my print line looks like this

print('Temperature = {0:0.0F}*c'.format(temp, c_to_f(temp))),

where c_to_f() converts temp from farenheit into centrigrade

What must I do to 'over-print' the line? (python 2.7.9)

Many thanks
Adrian

User avatar
DougieLawson
Posts: 37096
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: RPi / python print format

Tue Mar 07, 2017 8:20 am

Code: Select all

#!/usr/bin/python

import sys
import time

for i in range(0,101,10):
 sys.stdout.write("\r[%3d%%]" % i)
 sys.stdout.flush()
 time.sleep(1)

sys.stdout.write("\n")
sys.stdout.flush()

for i in range(0,10):
 sys.stdout.write("\rBlinking ...")
 sys.stdout.flush()
 time.sleep(0.5)
 sys.stdout.write("\r         ...")
 sys.stdout.flush()
 time.sleep(0.5)

sys.stdout.write("\n")
sys.stdout.flush()
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

adrian5750
Posts: 27
Joined: Mon Mar 06, 2017 10:10 pm

Re: RPi / python print format

Tue Mar 07, 2017 8:36 am

HI Dougie
Thanks for the reply.
So I need to use stdout() rather than print() ?

I've been searching the 'net for answers, and was hoping to add a character to my print() statement that would give me a <cr> without a <lf> - but the best I've managed so far is to supress the default 'newline' with the comma at the end of the statement..

It's been a long time since I did much serious coding (1980's - Texas Assembler, lots of Texas Assembler),
and it's taking a little while for the 'coding' brain cells to wake up again!

By default, this .py file seems to be running in Python 2.7
Is print formatting any easier in Python 3? - should I use that instead?

Thanks for your help
Adrian

Thanks
Adrian

User avatar
DougieLawson
Posts: 37096
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: RPi / python print format

Tue Mar 07, 2017 8:42 am

Just try running that example, then use sys.stdout.write and sys.stdout.flush into your code.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

adrian5750
Posts: 27
Joined: Mon Mar 06, 2017 10:10 pm

Re: RPi / python print format

Tue Mar 07, 2017 9:14 am

Thanks - I'll give it a try later.
Adrian

adrian5750
Posts: 27
Joined: Mon Mar 06, 2017 10:10 pm

Re: RPi / python print format

Tue Mar 07, 2017 1:47 pm

HI Dougie
Thanks for the code. I saved it as a .py file on my machine.
When I run it, I get a display on my screen of

[ 0%] [ 10%] [ 20%] and so on..

What I was looking for was something that printed

[ 0%]
and then overwrote that, in the same position on the screen, with [ 10%] and then [ 20%]
and so on...

which isn't quite the same thing.
Have I misunderstood?
Thanks
Adrian

User avatar
DougieLawson
Posts: 37096
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: RPi / python print format

Tue Mar 07, 2017 2:47 pm

When I run it the next percentage overlays the original and the second half of the program has blinking text.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

adrian5750
Posts: 27
Joined: Mon Mar 06, 2017 10:10 pm

Re: RPi / python print format

Tue Mar 07, 2017 3:08 pm

Hi Dougie
Thanks for the reply.
I tried running your script under Python3 and it performs as expected - with the overwrites and blink..
but not under Python2.7.9 on my Pi
Maybe I should use Python3 by default?

I tried running my original program using python3 myscript.py, but it complained that a module 'Adafruit_GPIO' was missing (this worked while running under Python2..
I guess I need to tell Python3 about the module, somehow?

Thanks for your assistance
Adrian

ghp
Posts: 1456
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: RPi / python print format

Tue Mar 07, 2017 4:03 pm

Hello,
to make simple things complicate: there is the possibility to use the terminal window with block graphics. Good old world, you know from fairy tales...
The magic module is 'curses' and is the de-facto standard for portable advanced terminal handling.

I found a sample in https://de.wikibooks.org/wiki/Python_un ... ux:_Curses which is in german, but I think there are more tutorials also in english.
The code opens a pseudo window and displays some values. Start in terminal window.

Code: Select all

#!/usr/bin/python
# -*- coding: utf-8 -*-
import curses
import time

stdscr = curses.initscr()
try:
    # do not show pressed keys
    curses.noecho()
    # no lin ebuffer
    curses.cbreak()
    # Escape-Sequence
    stdscr.keypad(1)
    
    # Farben
    curses.start_color()
    curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLUE)
    curses.init_pair(2, curses.COLOR_YELLOW, curses.COLOR_BLACK)
    
    # Fenster und Hintergrundfarben
    stdscr.bkgd(curses.color_pair(1))
    stdscr.refresh()
    
    win = curses.newwin(5, 30, 5, 5)
    win.bkgd(curses.color_pair(2))
    win.box()
    for i in range(0,10):
        win.addstr(2, 2, "value is {value:d}".format(value=i))
        win.refresh()
        time.sleep(1)
    
    win.addstr(2, 2, "press key to exit" )
    win.refresh()
    
    # wait for key press
    c = stdscr.getch()
finally:
    # ensure the terminal is reset to a useable state
    curses.nocbreak()
    stdscr.keypad(0)
    curses.echo()
    curses.endwin()
    print("finished")
Hope this helps,
Gerhard

adrian5750
Posts: 27
Joined: Mon Mar 06, 2017 10:10 pm

Re: RPi / python print format

Tue Mar 07, 2017 4:28 pm

HI Gerhard

Thank you for your reply...

Actually, I've made some progress using Dougie's sys.stdout.write scheme.

Downloaded & installed the Adafruit_GPIO, so that Python3 could 'see' it

Largely by trial & error (!) managed to get stdout.write to display the output from the thermocouple / MAX31855 so that it overwrites in the same position on the screen...

Success!
Thanks
Adrian

User avatar
DougieLawson
Posts: 37096
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: RPi / python print format

Tue Mar 07, 2017 4:31 pm

adrian5750 wrote:Hi Dougie
Thanks for the reply.
I tried running your script under Python3 and it performs as expected - with the overwrites and blink..
but not under Python2.7.9 on my Pi
Maybe I should use Python3 by default?
All new code should be python3.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Return to “Python”