0

layer.renameAttribute(0,'xxx') Traceback (most recent call last): File "", line 1, in AttributeError: 'QgsVectorLayer' object has no attribute 'renameAttribute'

Can you get me solution for this error?

closed as off-topic by PolyGeo Jan 25 '18 at 20:21

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions seeking help to debug/write/improve code must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Providing a clear problem statement and evidence of a code attempt will help others to help you. See: How to create a Minimal, Complete, and Verifiable example." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    The QgsVectorLayer::renameAttribute() method was introduced in QGIS 2.16. I'm guessing you're using an earlier version? – Joseph Jan 25 '18 at 10:24
  • yes i'm using 2.14.3 – Karthikeyan Jan 25 '18 at 11:05
  • Could you update to a more recent version (e.g. 2.18)? Otherwise you could try the method posted here. – Joseph Jan 25 '18 at 11:08
2

I had the same issue. Try this piece of code. It worked for me.

layer = iface.activeLayer()
for field in layer.pendingFields():
    if field.name() == 'old':
        with edit(layer):
            idx = layer.fieldNameIndex(field.name())
            layer.renameAttribute(idx, 'new')
  • no tried this too,but same error is generating every time,do i need to import ant file for this to run? – Karthikeyan Jan 25 '18 at 10:44
  • This code is based on QGIS 3.0 not on 2.0 which causes the error in QGIS < 3.0 – Chetan Vashisth Nov 12 at 12:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.