0

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.

6
  • Hmm. Works for me on MacOS and a RHEL Linux VM. Commented Jul 15, 2021 at 15:21
  • works for me on Ubuntu 18.04 with Python 3.9.5 and Python 3.6.9. Note that the error comes from the character Commented Jul 15, 2021 at 15:29
  • Hmm. Works for me on Windows10, WSL Ubuntu 20.04.1 LTS, Python 3.8.6 and os.getenv("PYTHONIOENCODING") returning 'utf-8' Commented Jul 15, 2021 at 16:40
  • Possibly helpful: stackoverflow.com/a/57224678/5320906, stackoverflow.com/a/54599110/5320906 Commented Jul 16, 2021 at 7:25
  • As commented by @snakecharmerb following solution helped: stackoverflow.com/a/57224678/16399680 Commented Jul 16, 2021 at 8:26

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.