5

I'm trying to select features with a specific text that I have in an attribute. I used the expressions from the PyQGIS Cookbook but I'm having trouble with the selection because the functions I'm using don't select any feature and I don't get any error message. If I change the selected attribute for another attribute which is formatted as a number instead of text, it works, so I assume the problem is related to the string format. Does anyone know how to use the string attribute correctly, so that it select the records in a specified attribute? Below is the code I'm using:

query = '"System" = "1x10/6 rot"'
selection = layer.getFeatures(QgsFeatureRequest().setFilterExpression(query))
layer.selectByIds([s.id() for s in selection])

I was also trying to use the LIKE operator, but also without success (no selected features):

query = '"System" LIKE "1x10%"'
selection = layer.getFeatures(QgsFeatureRequest().setFilterExpression(query))
layer.selectByIds([s.id() for s in selection])

However, when I use an attribute which is formatted as number it works:

query = '"fid" < 7'
selection = layer.getFeatures(QgsFeatureRequest().setFilterExpression(query))
layer.selectByIds([s.id() for s in selection])
2
  • 1
    1x10/6 rot looks like some encoding error, are you sure this is how the string is supposed to look like in the attribute table?
    – Bera
    Commented Feb 3, 2022 at 9:15
  • 2
    Yes, this is a predefined value (text) in attribute that I should use for selection, but I think the problem may come from / (slash sign) or the text in general, because when I try to use other attributes that contain only A-Z letters, it also does not work.
    – caro
    Commented Feb 3, 2022 at 9:20

3 Answers 3

7

You can use python like this:

layer = QgsProject.instance().mapLayersByName('New scratch layer')[0] #Change to match your layername...
fieldname = 'System' #...and fieldname

for f in layer.getFeatures():
    if f[fieldname]==r'1x10/6 rot':
        layer.select(f.id())

enter image description here

0
5

You have to use single quote for strings.

query = '"System" = \'1x10/6 rot\''
query = '"System" LIKE \'%1x10%\''

You can also remove double quotes from the field name.

query = "System = '1x10/6 rot'"
query = "System LIKE '%1x10%'"
2
  • 1
    Thank you for your input. Unfortunatelly, any of the suggested solutions do not select any feature in my data. Do you know is there any other way to solve this issue? Or where the problem may come from?
    – caro
    Commented Feb 3, 2022 at 9:32
  • I am also failing to get this to work, specifically with regards to finding a string using a setFilterExpression. Have tried all above combinations of quotes/removing quotes, but no luck.
    – nr_aus
    Commented Mar 8, 2023 at 2:33
2

There is another thread which deals with this

Trying to use LIKE in an expression in pyQgis

But the answer that worked for me was to use the 'triple quotes' solution, whereby I copied the expression as it works in the Expression Filter GUI into the code, and then encompass it with """ at the start and end (and also a few spaces just to make formatting a bit neater)

So for this particular example, the code would be:

query = """ "System" = '1x10/6 rot' """
selection = layer.getFeatures(QgsFeatureRequest().setFilterExpression(query))
layer.selectByIds([s.id() for s in selection])

This enables the filtering to work via the expression and QgsFeatureRequest method, without having to loop over the records.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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