1

I am selecting a river by expression. There are more than two rivers with the same name. So, I am thinking of using HUC. I found the following site about the expression: Selecting features using an expression with PyQGIS But I could not get a valid syntax if there are two expressions. My code is the following. How do I fix it?

layer.selectByExpression("\"PNAME\"='{}'".format(river_name)AND "\"HUC\" = '{}'".format(huc)) 
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
hiroaki
  • 187
  • 7

1 Answers1

2

I like to list ids and select by them. I think the python syntax is easier to get right:

lyr = iface.activeLayer()
field1 = 'kommunnamn'
field2 = 'lan_kod'

ids = [f.id() for f in lyr.getFeatures(QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry)) if f[field1]=='Ale' and f[field2]=='14'] lyr.select(ids)

enter image description here

BERA
  • 72,339
  • 13
  • 72
  • 161