I've developed a plugin for QGIS 2.12 which uses data that is encoded in UTF-8. The plugin works fine for 2.12, but when I try to start it in QGIS 2.14 it throws me the following error:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 4: ordinal not in range(128)
The code snippet which causes the exception is
value = QLabel(str(feature[field]).decode('utf-8'))
.
In former cases the error was thrown because I forgot to decode the characters, but I do convert them in that case. I already tried to remove the str()
and the .decode('utf-8')
calls, but the exception still gets thrown.
Why does this work for QGIS Lyon but not for QGIS Essen?
print(feature[field])
?E.-Mühsam-Str.
I think python tries to convert it into ascii as it is the standard encoding, but I also added# -*- coding: utf-8 -*-
at the top of every file. I resolved the issue just now by explicitly setting the coding to uft-8:reload(sys) sys.setdefaultencoding('utf-8')
Now it works, although that might not be the most convenient solution.. Nevertheless thank you for your answer!