I want to select features from a layer (called Road) with two fields ( A and B) with a certain condition. I want to get features with (A = MyA and B =MyB). A and B are string.

This is my code. However, I don't know why it only selects the features according to the first part (A = MyA) and ignors the AND ?? Do you know how to use AND operator in SelectbyExpression??

MyA = T[1]
MyB =T[2]

Road.selectByExpression(" \"A\" = '{}' ".format(MyA) and " \"B\" = '{}' ".format(MyB), QgsVectorLayer.SetSelection)

selectedRoad= Road.selectedFeature()

closed as off-topic by Vince, Fran Raga, LaughU, Jochen Schwarze, Erik Aug 27 at 7:25

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This problem cannot or can no longer be reproduced. Changes to the system or to the asker's circumstances may have rendered the question obsolete, or the question does not include a procedure to enable potential answerers to reproduce the same symptoms. Such questions are off-topic as they are unlikely to help future readers, but editing them to include more details can lead to re-opening." – Vince, Fran Raga, LaughU, Jochen Schwarze, Erik
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Use one string, with the and inside and two arguments to one .format -- Right now you're doing an and on strings. – Vince Aug 26 at 2:04
  • Could you please edit my code so I understand how to do it? – Shiva Aug 26 at 2:22
  • Thanks @Vince I found it should be like this :Road.selectByExpression(" \"A\" = '{}' and "\"B\"='{}' ".format(MyA,MYB),QgsVectorLayer.SetSelection) – Shiva Aug 26 at 2:38

Browse other questions tagged or ask your own question.