Sunday 21 January 2018

python - How can I avoid UnicodeEncodeError when using geoprocessing tools in QGIS?


I have a shapefile, that I want to split with the "Split Layers by Field"-tool. Due to some umlauts (ä,ü,ö) there will be a python error as soon, as the tool reaches a feature with an umlaut. The following error message will appear:



*An error has occured while executing Python code: Traceback (most recent call last): File "C:/Users/Gidi/.qgis//python/plugins\layers_by_field\layers_by_field_dialog.py", line 58, in accept self.split( inLayer, inField ) File "C:/Users/Gidi/.qgis//python/plugins\layers_by_field\layers_by_field_dialog.py", line 146, in split self.vlayer = QgsVectorLayer(vProvider.dataSourceUri(), str(layer.name()) + "_" + str(uValues[j]), "ogr") UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 7: ordinal not in range(128) Python version: 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] QGIS version: 1.8.0-Lisboa Lisboa, 6416f38 Python path: ['C:/PROGRA~2/Quantum GIS Lisboa/apps/qgis/./python', 'C:/Users/Gidi/.qgis//python', 'C:/Users/Gidi/.qgis//python/plugins', 'C:/PROGRA~2/Quantum GIS Lisboa/apps/qgis/./python/plugins', 'C:\PROGRA~2\Quantum GIS Lisboa\bin\python27.zip', 'C:\PROGRA~2\Quantum GIS Lisboa\apps\Python27\DLLs', 'C:\PROGRA~2\Quantum GIS Lisboa\apps\Python27\lib', 'C:\PROGRA~2\Quantum GIS Lisboa\apps\Python27\lib\plat-win', 'C:\PROGRA~2\Quantum GIS Lisboa\apps\Python27\lib\lib-tk', 'C:\PROGRA~2\Quantum GIS Lisboa\apps\qgis\bin', 'C:\PROGRA~2\Quantum GIS Lisboa\apps\Python27', 'C:\PROGRA~2\Quantum GIS Lisboa\apps\Python27\lib\site-packages', 'C:\PROGRA~2\Quantum GIS Lisboa\apps\Python27\lib\site-packages\PIL', 'C:\PROGRA~2\Quantum GIS Lisboa\apps\Python27\lib\site-packages\win32', 'C:\PROGRA~2\Quantum GIS Lisboa\apps\Python27\lib\site-packages\win32\lib', 'C:\PROGRA~2\Quantum GIS Lisboa\apps\Python27\lib\site-packages\Pythonwin', 'C:\PROGRA~2\Quantum GIS Lisboa\apps\Python27\lib\site-packages\wx-2.8-msw-unicode', 'C:\PROGRA~2\Quantum GIS Lisboa\apps\qgis\python\plugins\fTools\tools']*




What can I do to avoid this error? I do not want to change the name of every feature and would prefer the umlauts to remain in my shapefile.



Answer



The problem is in the python code. The following line of the error message highlights the problem:



"C:/Users/Gidi/.qgis//python/plugins\layers_by_field\layers_by_field_dialog.py", line 146, in split self.vlayer = QgsVectorLayer(vProvider.dataSourceUri(), str(layer.name()) + "_" + str(uValues[j]), "ogr") UnicodeEncodeError: 'ascii' codec can't encode character u'\xfc' in position 7: ordinal not in range(128) Python version: 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] QGIS



The method str() is used there to generate a string. Instead, the method unicode() should be used to generate a unicode string. Note: this only applies to QGIS 2, starting from QGIS 3 this no longer is applicable due to the usage of Python 3 where str already is unicode by default.


self.vlayer = QgsVectorLayer( vProvider.dataSourceUri(), unicode(layer.name()) + "_" + unicode(uValues[j]), u'ogr')

If it is not your own plugin, please file a bug or contact the author.



Edit: I just checked.Here is the link to the bugtracker for this plugin.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...