Select to view content in your preferred language

Python CalculateField sometimes doesn't work depending on field value variable

1289
10
12-30-2013 01:28 PM
PamFroemke
Deactivated User
I'm trying to add and calculate a text field in an existing dbf table by using a variable to set the value of the field. The AddField code is working fine. The CalculateField code sometimes does not work depending on the value of the variable I'm using to calculate the field.


My statements (inside a for loop) to add and calculate the field are:
for ascii in rasterList:[INDENT]outTable = r'D:\test.dbf'[/INDENT]
[INDENT]dateField = 'Date'[/INDENT]
[INDENT]fieldLength = 20[/INDENT]
[INDENT]part = os.path.split(ascii)[1][/INDENT]
[INDENT]fieldValue = os.path.splitext(part)[0][/INDENT]
[INDENT]arcpy.AddField_management(outTable, dateField, "TEXT", "", "", fieldLength)[/INDENT]
[INDENT]arcpy.CalculateField_management(outTable, dateField, fieldValue)[/INDENT]


If I set fieldValue = os.path.splitext(part)[0], it does not work--the field remains blank. If I print fieldValue, I get ECR_TXT_201304, which is correct--it's what I want stored in the Date field for all records in the table.
If I set fieldValue = "201304", it works--the Date field gets populated with 201304.
I even tried fieldValue = str(os.path.splitext(part)[0]), and that didn't work either.
I never get any error messages, the script runs without any indication of a problem.

I'm baffled at this point why this code doesn't work when using the os.path variable.
Thank you for any help!
Tags (2)
0 Kudos
10 Replies
PamFroemke
Deactivated User
Sol, your solution with the "+" symbols worked. Not sure why the syntax has to be this cumbersome. Seems like I used to be able to use syntax like my original script. I guess this is the new and "improved" Python?

Thanks, everyone, for the help!!
0 Kudos