To jest prostsze niż w wersji 1.8:
layer = qgis.utils.iface.activeLayer()
fields = layer.pendingFields()
field_names = [field.name() for field in fields]
lub w jednej linii
field_names = [field.name() for field in layer.pendingFields() ]
po
for elem in layer.getFeatures():
print dict(zip(field_names, elem.attributes()))
{u'adip': 17, u'dipdir': 130, u'tdip': 29}
{u'adip': 55, u'dipdir': 325, u'tdip': 75}
.....
Zasadniczo do eksploracji nowej funkcji używam dir () lub modułu see , aby sprawdzić, co jest w środku:
dir(fields)
['FieldOrigin', 'OriginEdit', 'OriginJoin', 'OriginProvider', 'OriginUnknown', '__class__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__len__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'append', 'at', 'clear', 'count', 'extend', 'field', 'fieldOrigin', 'fieldOriginIndex', 'indexFromName', 'isEmpty', 'remove', 'size', 'toList']
lub
see(fields)
[] hash() help() len()
repr() str() .FieldOrigin() .OriginEdit
.OriginJoin .OriginProvider .OriginUnknown .append()
.at() .clear() .count() .extend()
.field() .fieldOrigin() .fieldOriginIndex()
.indexFromName() .isEmpty() .remove() .size()
.toList()
I widać, że istnieje funkcja field ()
print fields.field(0)
qgis.core.QgsField object at 0x163E39C0
a pole ma nazwę, typ, precyzję, ...:
see(fields.field(0))
< <= == != >
>= hash() help() repr()
str() .comment() .displayString() .length()
.name() .precision() .setComment() .setLength()
.setName() .setPrecision() .setType() .setTypeName()
.type() .typeName()
więc:
fields.field(0).name()
u'adip'
fields.field(0).type()
2
fields.field(0).typeName()
u'Integer'
fields.field(0).precision()
0
....
z .dataProvider ()
Jest to .field ()
....
prov = layer.dataProvider()
prov.fields().field(0).name()
u'adip'
prov.fields().field(0).typeName()
u'Integer'
i
field_names = [field.name() for field in prov.fields()]