Following code executes fine on my WIN10, Pycharm Laptop with Python 3.6:
import struct
from datetime import datetime
from pytz import UTC
from django.utils.encoding import smart_str
data = b'A\x10Vu\x87%\x00x\r\r\x03\x01\x00\x00\x00\x00$\x00\x00\x00\x00\x00\x00\x00\x00'
devid,hw,sw,bat,ss,mod,t1,dii,adc,t2,h,ts = struct.unpack('>6sBBHBBhBHhHL',data)
# fields that needed processing are done in the f-strings below
print(f"DeviceID={devid.hex()} HW={hw} SW={'.'.join(str(sw))}\n"
f"BAT={bat:.3f}mV SignalStrength={-113+2*ss}dBm Mode={mod} Temp={t1/10}\N{DEGREE CELSIUS}\n"
f"Door={dii==0x80} ADC={adc}mv Temp2={t1/10:.1f}\N{DEGREE CELSIUS} Humidity={h/10:.1f}%\n"
f"Timestamp={datetime.fromtimestamp(ts,UTC)}")
However, when I execute the same code on my virtual ubuntu machine, I get the following error message:
Traceback (most recent call last): File "decoderexample.py", line 11, in print(f"DeviceID={devid.hex()} HW={hw} SW={'.'.join(str(sw))}\n" UnicodeEncodeError: 'ascii' codec can't encode character '\u2103' in position 89
The Machine runs following python version:
Python 3.6.5
Distributor ID: Ubuntu Description: Ubuntu 18.04 LTS Release:
18.04 Codename: bionic
I have tried to use smart_str instead of str and I've also deleted the str statements from the print statement.