I'm trying to rename multiple field names (about 150) of a shapefile in QGIS. Their names are different but they all end by _xf20 and I would like to remove this part for each of them.
I've looked up to other topics that could help and I've found this one: Rename fields of shapefile using PyQGIS 3 with the code:
layer = iface.activeLayer()
Open editing session
layer.startEditing()
Rename field
for field in layer.fields():
if field.name() == 'oldName':
idx = layer.fields().indexFromName(field.name())
layer.renameAttribute(idx, 'newName')
Close editing session and save changes
layer.commitChanges()
and this one: Delete all fields with the string "RAWI" using QGIS Python
But even if it is a start, I'm a beginner with Python and I don't find a way to adapt them for my case.
I would rather not edit every single field manually.
if layer.name().startswith('Z10')to your script but it doesn't work (even thought I don't get any error message) – Linda Aug 22 '20 at 12:58