29

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

2
  • 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
    Commented Oct 16, 2018 at 19:07
  • Please, do not forget about "What should I do when someone answers my question?"
    – Taras
    Commented Nov 27, 2022 at 17:26

4 Answers 4

66

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 the QgsFeatureRequest and its "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 its name, rather than a number, you need to use the field mappings:

idx = feature.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

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 came in or if it's always been there.

21

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 (which corresponds to the first index), then I would do this:

layer = iface.activeLayer()

features = layer.getFeatures()

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

It uses the getFeatures() method of the QgsVectorLayer class and the attributes() method of the QgsFeature class.

8

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 layer 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.

0

To spell this out in a slightly different and more compact way, this is how you could do it with a given file path and desired row and column

yourLayer       = 'C:/Temp/SpatialFile.gpkg'
specifiedRow    = 2
specifiedColumn = 2

extractedValue = next(QgsVectorLayer(yourLayer).getFeatures(QgsFeatureRequest().setFilterFid(specifiedRow))).attributes()[specifiedColumn - 1]
print(extractedValue)

If you're wishing to use a for-loop over this then the bounds of the attribute table may be helpful

numberOfColumns = len(QgsVectorLayer(yourLayer).attributeList())
numberOfRows    = QgsVectorLayer(yourLayer).featureCount()

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