My aim is to write an algorithm, where the user selects a settlement name, and numeric value, from which a query is generated, and a selection proceeds on the selected layer, than the selection result is exported to a new shape. It works, but when the input settlement name includes a special character, it fails.

I have the following code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

##[Example scripts]=group
##nev=string Szeged
##depth=number 3000

from qgis.core import *
import processing

input = processing.getObject(lyr)

print depth
print name
l = input
expr = QgsExpression(" \"MELYSEG\">'{}' AND \"TLP\"LIKE'{}'".format(depth, nev))
it = input.getFeatures( QgsFeatureRequest( expr ) )
ids = [i.id() for i in it]
input.setSelectedFeatures( ids )

QgsVectorFileWriter.writeAsVectorFormat(input, "D:/Data/output.shp", "utf-8", input.crs(), "ESRI Shapefile", 1)

The field type of "MELYSEG" is a float the "TLP" is a string.

When I have an input like this (note the ő character in the name): enter image description here

I get the following error:

'ascii' codec can't encode character u'\u0151' in position 4: ordinal not in range(128) See log for more details

Any ideas to solve this?


What I always do to handle unicode in expressions is to prepend a u to the expression string, in this way:

expr = QgsExpression( u"\"MELYSEG\" > {} AND \"TLP\" LIKE '{}'".format( depth, nev ) )

Also note, as MELYSEG field is of type float, you don't need the single quotes for its value.

I suggest you to test your expressions directly in the QGIS GUI first, and only when you have an expression that works, implement it in PyQGIS.

| improve this answer | |

You need to add the encode() function for your string input.

So try replacing:

expr = QgsExpression(" \"MELYSEG\">'{}' AND \"TLP\"LIKE'{}'".format(depth, nev))

with this:

expr = QgsExpression(" \"MELYSEG\">'{}' AND \"TLP\"LIKE'{}'".format(depth, nev.encode(u'ISO-8859-2')))
| improve this answer | |

You need to use the encode() function in python:

expr = QgsExpression(" \"MELYSEG\">'{}' AND \"TLP\"LIKE'{}'".format(depth, nev).encode('ascii'))
| improve this answer | |
  • 6 second difference ;) – Joseph Dec 1 '16 at 12:20
  • :) but it seems yours is correct, I have added ascii but i think it should be utf as ascii is the one who failed to encode as per error message in the question. right ? – Shiko Dec 1 '16 at 12:21
  • I based the encoding on the OP's previous question so yours could be correct, just depends what the OP is looking for in the new question =) – Joseph Dec 1 '16 at 12:23
  • Thank you for your answers, I might be dummy, but altought there is no error message now, the script runs but without results. Sorry for the so many questions I am pretty new to pyqgis. – buboreka Dec 1 '16 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.