[Qgis-user] pyQGIS : read attributes with qgis 2.2?

Richard Duivenvoorde rdmailings at duif.net
Wed Jun 18 10:09:27 PDT 2014


On 18-06-14 16:58, dandrigo wrote:
> Thanks,
> 
> I would like back up attributes data. I tried to do thanks to following
> piece of code : 
> 
> -----------------------------------------
> provider = layer.dataProvider()
> layer.select(provider.attributeIndexes())
> for f in layer.getFeatures():
>     print f['nom','reference','id_espace','table_espace','id_utilisateur']
> -------------------------------------------
> 
> 
> But when i run the python extension, i can display the layer. But i can't
> see the attribut table yet...;-(

from:

http://docs.qgis.org/2.2/en/docs/pyqgis_developer_cookbook/vector.html

attrs = f.attributes()
# attrs is a list. It contains all the attribute values of this feature


# Attributes can be refered by index.
idx = layer.fieldNameIndex('name')
print feature.attributes()[idx]
# an alternative:
print f['name']

more info:
http://nathanw.net/2013/06/13/new-qgis-20-api/


Regards,

Richard





More information about the Qgis-user mailing list