1

Is it possible using Python to select all features whose attribute contain a specific string?

I am new to QGIS.

The layer has an address column ("Bauvorha_3") which looks like this:

Berlin, Am Alexanderplatz, ...

Berlin, Goethestrasse, ...

Hamburg, Alsterufer, ...

Hamburg, Reeperbahn, ...

I need to select all features containing for example "Hamburg" or "ufer".

place is the user-input string. I've tried to use LIKE but it doesn't seem to work for me.

place = self.dlg.lineEdit.text()
place = str(place)
versuch = "'{}'".format(place)
search = '"Bauvorha_3" LIKE "{}%"'.format(versuch)
self.iface.activeLayer().selectByExpression(search)
Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
Brian
  • 19
  • 1

1 Answers1

1

Try:

search = """"Bauvorha_3" LIKE('%{0}%')""".format(place)

You were missing enclosing () and % sign before keyword. And i f you have alot of single and double quotes inside a string, enclosing everything in three double quotes usually work to make python understand what you want.

BERA
  • 72,339
  • 13
  • 72
  • 161