0

I'm just trying to extract the variable value of the 7th column of the first row in a vector layer. I'm not sure how to index the table exactly though. I included the lines I wrote, which I imagine are pretty close. I think the third line is what needs tweaking?

 lyr = iface.activeLayer()
 features = lyr.getFeatures()

 attrs = features.attributes()
 var = attrs[7]

I found solutions where they go through a loop of all the features but I'm trying to avoid this if possible.

2
  • 1
    The seventh item in a zero-based list has a radix of six.
    – Vince
    May 20 at 14:34
  • Still doesn't work.
    – hmnoidk
    May 20 at 14:36
1

getFeatures() returns an iterator object. You can use instead:

feature = lyr.getFeature(0)

then you can fetch a list using feature.attributes(), or

lyr.getFeature(0).attribute(6) for directly extracting 7th column of first row.

1

A recipe to get first row and then 7th column

lyr = iface.activeLayer()

# Get First feature
feat1 = next(lyr.getFeatures())

var = feat1[6] # column index start at 0
# or
var = feat1[layer.fields().indexFromName('column7name')]

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.