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])
Taras
  • 32,823
  • 4
  • 66
  • 137
caro
  • 53
  • 4
  • 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 Feb 03 '22 at 09: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 Feb 03 '22 at 09:20

3 Answers3

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

BERA
  • 72,339
  • 13
  • 72
  • 161
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%'"
Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
  • 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 Feb 03 '22 at 09: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 Mar 08 '23 at 02: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.

nr_aus
  • 3,535
  • 6
  • 26