Skip to content

Instantly share code, notes, and snippets.

@SzieberthAdam
Last active January 19, 2016 10:44
Show Gist options
  • Save SzieberthAdam/c85ee367a17b307eea27 to your computer and use it in GitHub Desktop.
Save SzieberthAdam/c85ee367a17b307eea27 to your computer and use it in GitHub Desktop.
PyQGIS: Get fields of layer
# QgsFields() of QgsVectorLayer()
# fields = layer.fields() # from QGIS 1.12+
fields = layer.pendingFields() # and below...
# list QgsField() from QgsFields()
fieldlist = fields.toList()
# list of layer fieldnames
[f.name() for f in fieldlist]
# field attributes
fattr = ('name', 'type', 'typeName', 'length', 'precision', 'comment')
# print the field table of a layer
for f in fieldlist:
print '; '.join(['{!r}'.format(getattr(f, a)()) for a in fattr])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment