1

I need to rename programticaly some fields in an existing shapefile. The info is stored in the corresponding dbf file, so I have used dbf package with below code (it has been simplified to be shared but it is functional):

import os, dbf

HIGH_LEVEL = 2
LOW_LEVEL = 4
table = dbf.Table('Polygons.dbf')
table.open(mode=dbf.READ_WRITE)
try:
    for oldName in table.field_names:
        suf = int(oldName[-1:])
        if suf == HIGH_LEVEL:
            newName = oldName.upper().replace(str(HIGH_LEVEL), 'high')[:10]
            print(f"{oldName} -> {newName}")
            table.rename_field(oldName, newName)
        if suf == LOW_LEVEL:
            newName = oldName.upper().replace(str(LOW_LEVEL), 'low')[:10]
            print(f"{oldName} -> {newName}")
            table.rename_field(oldName, newName)
finally:
    table.close()

But the result is all characters lowercase. Having a look into the dbf package, rename_field function, the every first thing that it does is to lowercase old name and new name:

def rename_field(self, oldname, newname):
    """
    renames an existing field
    """
    oldname = oldname.lower()
    newname = newname.lower()
    ...

You can comment both lines (I don't recommend to modify packages but just for testing) and then you will get the first rename done but the second will fails because the first one is readed in lowercase by dbf package and it cannot found it.

In the other hand, using QGIS, the manual renaming with mixed case can be done, so it is not a limitation of dbf files itself rather than dbf package limitation.

So, anybody can point me to a python module that allow me to rename dbf field names with mixed case?

Thanks a lot in advance.

Nando
  • 11
  • 3

1 Answers1

0

The field names are lower-cased for presentation only. They are stored in the file as upper-case, and you can access them as uppercase (since v0.98.1).

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237