More

How can I check in QGIS python if an atrribute is included in my layer?

How can I check in QGIS python if an atrribute is included in my layer?


I use the following code:

for field in self.layer.pendingFields(): if field.name()=='elevation': do this else: do that

Although there is an elevation attribute in my layer it does not recognize it does that instead of this. Any ideas why?


Your approach is calling either this or that for every single field in the attribute table. That might mess things up. Instead try

field_names = [field.name() for field in self.layer.pendingFields()] if 'elevation' in field_names: do this else: do that

Watch the video: QGIS using Python Programming for Beginners