2
while True:
    volts = adc.readADCDifferential01(4096, 8)
    print volts

This works fine except that it prints out a column of voltages that quickly fills up the terminal screen. I'd rather it print the voltages side by side and fill up rows from left to right.

I tried putting a comma after the print volts but nothing shows up on the screen until I stop the program by pressing control-C. The comma does cause the voltages to be printed in rows but I need to watch the readings live instead of blindly waiting until I suspect the test is finished.

Why did adding a comma cause the program to stop showing the voltages as they are happening?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Rico
  • 329
  • 3
  • 9

1 Answers1

4

Python opens stdout in line-buffering mode, so you won't see voltages printed in columns until a flush, when not printing newlines.

Manually flush the buffer with:

import sys

while True:
    volts = adc.readADCDifferential01(4096, 8)
    print volts,
    sys.stdout.flush()
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343