1

I found a code that prints all data that a shapefile has in the attribute table in the qgis console. But i wanted to have a code that just printed the data that i select in the shapefile. Can someone help me?

I'll leave here the code that prints all the data.

lyr = iface.activeLayer()

features = lyr.getFeatures()

featList = []

selected_features = layer.selectedFeatures()

for feat in features:

featList.append(feat.attributes())

featList.sort()

for f in featList:

print f
4

You almost have it... just loop over selected_features and indent the code properly:

lyr = iface.activeLayer()

selected_features = lyr.selectedFeatures()
featList = []


for feat in selected_features:
  featList.append(feat.attributes())

featList.sort()

for f in featList:
  print f

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.