[SOLVED] Jython unicode, ascii problem

Here ist my code:

util.py:

from core.log import logging, LOG_PREFIX
from core.jsr223.scope import actions
from configuration import admin_email, telegram_bot

def send_info_telegram(message):
	out = str(message)
	logging.getLogger("{}.Jython_TEST".format(LOG_PREFIX)).info(out.format(5 + 5))
	telegramAction = actions.get("telegram", telegram_bot)
	telegramAction.sendTelegram(out)    

test.py:

from core.log import logging, LOG_PREFIX  #, log_traceback
from configuration import admin_email, telegram_bot
import personal.util
reload(personal.util)  
from personal.util import send_info_telegram

AlarmWert = 50
AlarmwertQuantityType = str(AlarmWert) + u" °C"

Meldungstext = "Sensors > " + AlarmwertQuantityType
send_info_telegram(Meldungstext)     # line 28

result in openhab.log:

2020-03-05 13:16:32.245 [ERROR] [ion.module.script.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/python/personal/test.py': UnicodeEncodeError: 'ascii' codec can't encode character u'\xb0' in position 23: ordinal not in range(128) in <script> at line number 28

how do I change util.py so that I can pass unicode characters?

the following code does not cause any problems:

LOG = logging.getLogger("{}.Jython_TEST".format(LOG_PREFIX))
LOG.info(AlarmwertQuantityType)
2020-03-05 13:26:28.236 [INFO ] [jsr223.jython.Jython_TEST                           ] - 50 °C

I’m no Python expert but perhaps this produces an ASCII encoded string (or tries to). Perhaps it works if you replace it with out = unicode(message).
BTW, where exactly is line 28?

1 Like

I have marked the line above.

your solution works! thanks a lot!

Here is some reading…

https://docs.python.org/2.7/howto/unicode.html