[Qgis-developer] How to get the field values in PyQGis 2 ?

Nathan Woodrow madmanwoo at gmail.com
Wed Oct 23 02:42:15 PDT 2013


Simple just:

For feature in layer.getFeatures():
     For attr in feature:
          Print attr

If you have field names

For feature in layer.getFeatures():
       Print feature["field name"]

> On 23 Oct 2013, at 7:21 pm, Geo DrinX <geodrinx at gmail.com> wrote:
> 
> OK, I found it !
> 
> 
>                     iter = layer.getFeatures()
>                     for feat in iter:
>                       fff = feat.fields()
>                       num = fff.count()
>                       for iii in range(num):
>                          print "%s " %(feat[iii])
> 
> 
> But...  what about to have the field names ?
> 
> Thank you
> 
> Roberto
> 
> 
> 2013/10/23 Geo DrinX <geodrinx at gmail.com>
>> Hello all,
>> 
>> I have difficulties writing the python code to print the field value contents from a feature.
>> 
>> This is my source code:
>> 
>>                     iter = layer.getFeatures()
>>                     for feat in iter:
>>                       fff = feat.fields()
>>                       num = fff.count()
>>                       print "num %d\n" %(num)
>>                       for iii in range(num):
>>                          fieldValue = fff[iii]
>>                          print "%s\n" %(fieldValue)
>> 
>> but this is what I had:
>> 
>> <qgis.core.QgsField object at 0x000000000BF95268>
>> 
>> etcetera....
>> 
>> 
>> What I need to modify in the source code to have the correct string values ?
>> 
>> Sorry, but the 2.0 documentation is not so clear... actually  :(
>> 
>> 
>> Thank you in advance for any info about this
>> 
>> Roberto 
> 
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20131023/ba14fb06/attachment.html>


More information about the Qgis-developer mailing list