1

I have a shapefile with ~100 fields. Each field corresponds to a date, the name of each field is something like this 2006-07-05 (year-month-day).

I want to remove the - symbol and have only 20060705. I know I can edit the name in the properties options. I don't want to repeat this ~100 times.

Is there any way to do it programatically?

1
  • 1
    Note that it is bad practice to use all numbers for column ( or table name) or even just starting with a number, it's better to prefix with something....
    – J.R
    Commented Jul 7 at 16:50

2 Answers 2

4

You should be able to use the "Refactor Fields" tool but it will output a new layer.

You can use PyQGIS and .renameAttribute() method. The code below is going to replace all - with nothing/an empty string in all columns with a - in the name.

⚠️ Backup your layer first, the code will change the layer in place.

layer = QgsProject.instance().mapLayersByName("datenames")[0] #Create a layer object, ...
# ... replace datenames with the name of your layer.

for field in layer.fields(): #For each field in your layer
    if "-" in field.name(): #If there is a - in the field name
        new_name = field.name().replace("-", "") #Create a new name by replacing
        #  - with nothing
        with edit(layer): #Start an edit session
            idx = layer.fields().indexFromName(field.name()) #Find the fields index
            layer.renameAttribute(idx, new_name) #Rename the field by using the index and the new name

enter image description here

1
  • So. the order of fields will also be changed in your solution?
    – Taras
    Commented Jul 9 at 19:29
1

A one-time operation

There is a QGIS Plugin available for this purpose "Rename Field" with a simple interface. Can be found under Plugins > Rename Field > Rename field.

solution1

A multiple-time operation

It is also a PyQGIS approach, a bit similar to what @Bera suggested.

It utilizes the renameAttributes() method inherited from the QgsVectorDataProvider class.

⚠️ Do not forget to backup your layer first, the code will change the layer in place.

# imports
from qgis.core import QgsProject

# getting a layer by its name
layer = QgsProject.instance().mapLayersByName("YOUR_LAYER_NAME")[0]
# accessing data provider of the layer
dp = layer.dataProvider()

# a dict to store field indexes and their new names
new_fields = {}

# looping over each field in the layer
for field in layer.fields():
    name = field.name() # getting field original name
    if "_" in name: # matching condition
        ind = dp.fieldNameIndex(name) # finding field index
        new_fields[ind] = name.replace("_", "")

# changing field names
dp.renameAttributes(new_fields)
# updating the original layer
layer.updateFields()

Note: The order of fields visualized in the attribute table might be changed.


References:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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