[Tutor] Error in printing out totalSystolic on paper (1018)

Peter Otten __peter__ at web.de
Sun Aug 10 16:49:18 CEST 2014


Ken G. wrote:

> Receiving the following error from the terminal screen:
> 
> Traceback (most recent call last):
>    File "Blood Pressure05Print45.py", line 95, in <module>
> pr.write(totalSystolic)
> TypeError: expected a character buffer object
> 
> Portion of strip:
> =================
> 
>      pr.write (" "), pr.write (pulse), pr.write ("\n")
>      totalSystolic = totalSystolic + int(systolic)
>      totalDiastolic = totalDiastolic + int(diastolic)
>      totalPulse = totalPulse + int(pulse)
> file1.close()
> print
> print totalSystolic, totalDiastolic, totalPulse
> 
> # sys.exit()
> 
> pr.write(totalSystolic)
> # pr.write(totalDiastolic)
> # pr.write(totalPulse)
> 
> ==========================
> 
> I know that totalSystolic is 1018, totalDiastolic is 469 and
> totalPulse is 465.
> 
> I can not figure out why pr.write won't print out 1018 on paper.
> 
> Using Python 2.7 on Ubuntu 12.04.5 and using Geany to process the strip.
> 
> Thanks for your suggestion.

Assuming that pr is a file object the write() method accepts only strings. 
You are passing an integer. To fix your code you can either convert 
explicitly

pr.write(str(totalSystolic))

or implicitly

print >> pr, totalSystolic 

(as usual print appends a newline).



More information about the Tutor mailing list