16

What is the way to get the information contained in the attribute table, say in column 2, row 2 using pyqgis?

  • This is helpful, but this only seems to give you the value of the attributes. It seems attributes need to be a name/value pair. How do you get the name for each attribute? Is that what the "fields" is all about? – ajpieri Oct 16 '18 at 19:07
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. - From Review – Martin Hügi Oct 16 '18 at 19:37
41

To loop over every feature in a layer, use the getFeatures() generator. This returns an iterator of features (QgsFeature) in the layer.

for feature in layer.getFeatures():
    pass # do something with feature

If you're only interested in a particular feature, instead of all of the features in the layer, you can access it using a QgsFeatureRequest and it's FID:

fid = 1 # the second feature (zero based indexing!)
iterator = layer.getFeatures(QgsFeatureRequest().setFilterFid(fid))
feature = next(iterator)

Once you have a QgsFeature object you can use the attributes() method to retrieve the attributes (a.k.a. columns or fields) associated with that feature (a.k.a. row), e.g.:

# get the feature's attributes
attrs = feature.attributes()
# print the second attribute (note zero based indexing of Python lists)
print(attrs[1])

If you want to index the field by it's name, rather than a number, you need to use the field mappings:

idx = layer.fieldNameIndex('name')
print(feature.attributes()[idx])

The field index shouldn't change during the loop, so you only need to call it once.

There is more information and examples in the PyQGIS cookbook: http://www.qgis.org/pyqgis-cookbook/vector.html#iterating-over-vector-layer

Update

You can access feature attributes much more easily by using the QgsFeature like a dict, e.g.

for feature in layer.getFeatures():
    name = feature["name"]

I'm not sure which version that came in or if it's always been there.

10

Thought I would actually add some code, as googling this problem returns this question... most people prefer a quick explanation or a code snippet to a link that leads to a page that has a lot of additonal information.

In order to get the information in a table you would first have to access the layer's features. In my case I put the features in a variable. Then you loop through the features, and for each feature you call its attributes, which can then be printed using their column index. For example, if I would like to get all the values in the second column, then I would do this:

lyr = iface.activeLayer()

features = lyr.getFeatures()

for feat in features:
    attrs = feat.attributes()
    print attrs[1]
1

The answers above only show how to do this for the active layer. I would think, in many cases you might want to find said attribute for a layer that is not the active layers or a layer checked in the Layers window. The code below will get a list of all layers that you have added to your Layers window (regardless if they are checked or active) and find the attribute at row 2, column 2.

from qgis.core import QgsProject

layer = QgsProject.instance().mapLayersByName("myLayerName")[0]
        value = layer.getFeature(2).attribute(2)

Note mapLayersByName returns a list of layers with the given name. I am assuming one exists for this example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.