Delete multiple fields in qgis use python?

المشرف العام

Administrator
طاقم الإدارة
I have many shapefiles to edit. There are many fields in them to delete because I don't want them anymore. If script delete all fields I think it is good. After run it, I will put only one field that I want. I am trying to make there script that do it for all shapefiles, but, until now, I haven't reached there yet.

Under, I put two scripts that I have. Number 1 I have done it to delete specific field (defined by index). Number 2 is begin of the one which would delete all field at once.

for lyr in allLayers:

lyr.updateFields()lyr = iface.activeLayer()lstDelete = []for idx in lyr.dataProvider().attributeIndexes(): uv = lyr.dataProvider().uniqueValues( idx ) if not uv: lstDelete.append( idx )lyr.dataProvider().deleteAttributes( lstDelete )True# Update the layer structurelyr.updateFields()for vectorLayer in allLayers: columns = layer.pendingAllAttributesList() type(columns) print columns caps = layer. dataProvider() . capabilities()

for i in columns: print i if caps & QgsVectorDataProvider. DeleteAttributes: res = vectorLayer. dataProvider() . deleteAttributes() vectorLayer. updateFields()

أكثر...
 
أعلى