1

I'm a complete beginner trying to update some fields in an attribute table (i.e. an ArcMap Shapefile) using Python tool. My goal it to update the values from this column to another date notation.

Right now the fields look like this:

91/02/25
93/01/21

96/06/14 ...

The dates need to be replaced to the following format MM/DD/YYY. There are also some spaces (as shown above in the example) that I would like to replace to something like "No date available".

This is what I wrote:

for row in updateCursor:

    theWrongDate = row.RPT_DTE
    theRightDateList = theWrongDate.split("/")
    year = '19' + str(theRightDateList[0])
    month = theRightDateList[1]
    day = theRightDateList[2]
    theWrongDate = month + '/' + day + '/' + year
    updateCursor.updateRow(row)

and Python gives me an error message:

"month = theRightDateList[1]
IndexError: list index out of range"
  • You're missing a "Y" in your format. Right now this looks like a pure Python question. – Vince Nov 20 '16 at 1:49
  • Just below the line where you create theRightDateList, add a line to "print(theRightDateList)". Does the console result look something like ['91','02','25'] ? – Nate Wanner Nov 20 '16 at 3:35
  • Hi, thanks for the response. Some fields contained a space that I replaced by "XX/XX/XXXX". I added the line print(theRightDateList) and it returned all records (example of one: [u'95', u'11', u'08']). – Nico13 Nov 20 '16 at 23:36
  • Now, my problem is that the dates are not updated in the column. – Nico13 Nov 20 '16 at 23:38
2

If you split an empty value, it won't have anything at index 1. You can use an if statement to check this:

for row in updateCursor:

    theWrongDate = row.RPT_DTE
    theRightDateList = theWrongDate.split("/")

    if len(theRightDateList) == 3:
        year = '19' + str(theRightDateList[0])
        month = theRightDateList[1]
        day = theRightDateList[2]
        theNewDate = "{0}/{1}/{2}".format(month, day, year)

        # verify I have the next couple lines correct
        row.setValue(RPT_DTE, theNewDate)
        updateCursor.updateRow(row)
  • Hello Nate, thanks for your help. I did change the space or empty value by the default "XX/XX/XXXX". It worked. However, the code does not change the date notation to MM/DD/YYYY. It remains as YY/MM/DD – Nico13 Nov 20 '16 at 23:40
  • Any suggestions why the below lines aren't updating the fields? theWrongDate = month + '/' + day + '/' + year updateCursor.updateRow(row) – Nico13 Nov 20 '16 at 23:41
  • @Nico13 It looks like you figured it out, but generally you need to make the changes by setting the value, and then save your changes by updating the row. If something in the code above needs to be different, please hit the "edit" button and make the changes so others in the future can see what worked. Thanks. – Nate Wanner Nov 21 '16 at 0:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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