Monday, 2 July 2018

pyqgis - QGIS Processing algorithm cannot deal with special characters in input


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
##lyr=vector
##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?



Answer



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.


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...