1

I am trying to change the name of a field by using Python code where the current name is in the format (e.g.) d2000_5000, and 5000 is the decimal year (0,5 y) added to 2000. So i think that this can be done with the Alter Field tool, by extracting characters, then doing 365*5000/10000 to obtain days, and then adding these days to the initial date of 2000: 2000-01-01 + days (within a date sum that takes into account years with 29 days in Feb).

Somebody can help me proposing a code snip to do it?

1
  • Welcome to GIS SE! As a new user be sure to take the Tour. For questions that involve code we ask that you show us where you are stuck with your own code by including a code snippet in your question. There is an edit button beneath your question which will enable you to do that and a {} button that enables you to format any highlighted code nicely.
    – PolyGeo
    Commented Jun 2, 2018 at 7:57

1 Answer 1

1

Use the datetime module:

def fixdate(oldfieldname):
    from datetime import datetime, timedelta
    import re

    #Extract numbers using re module and convert to decimal year
    newfieldname = [int(s) for s in re.findall(r'\d+', oldfieldname)]
    newfieldname[1] = newfieldname[1]/1000
    newfieldname = float(str(newfieldname[0])+"."+str(int(newfieldname[1])))

    #Use datetime module to go from decimal year to datetime and ddmmyyyy
    year = int(newfieldname)
    rem = newfieldname - year
    base = datetime(year, 1, 1)
    result = base + timedelta(seconds=(base.replace(year=base.year + 1) - base).total_seconds() * rem)
    return 'D'+result.strftime('%d%m%Y')

For example:

fixdate('d2000_5000')
'D02072000'

And to alter field name:

arcpy.AlterField_management(r'C:\database\feature_class123', 'd2000_5000', fixdate(
    'd2000_5000'))
5
  • @user3265206 Nice! Could you accept my answer with the checkbox?
    – Bera
    Commented Jun 2, 2018 at 8:51
  • Great! It works perfectly, thank you BERA. Now I an trying to apply in a general case by using wilcards for the different dates in a loop structure: for field in fieldList: #loop through each field if field.name == "Z****_****": #look for the name with the structure Znnnn_nnnn, where n is an integer arcpy.AlterField_management(fc, field.name, fixdate(field.name), fixdate(field.name)) Commented Jun 2, 2018 at 9:11
  • Ok! You can also limit listed fields with startswith, for example like this: gis.stackexchange.com/questions/284969/…
    – Bera
    Commented Jun 2, 2018 at 9:15
  • Great! It works perfectly, thank you BERA. Now I an trying to apply in a general case by using wildcards for the different dates in a loop structure, I tried with 'Z*', "Z*" an "Z********", where * in the more exact case ("Z*******") is an integer, but none of these methods worked: for field in fieldList: #loop through each field if field.name == "Z****_****": #look for the name with the structure Znnnn_nnnn, where n is an integer arcpy.AlterField_management(fc, field.name, fixdate(field.name), fixdate(field.name)) Could you propose another wildcard method? Commented Jun 2, 2018 at 9:19
  • @user3265206 I think you should post this as a new question. Add what you have tried to the question and it will not be put on hold
    – Bera
    Commented Jun 2, 2018 at 9:21

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