Select to view content in your preferred language

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

1288
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
RichardFairhurst
MVP Alum
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!


It is a very bad practice to name a field with a key word like 'Date'.  I am surprised it even accepts that field name.  That field name is bound to cause strange behaviors.  Name the field something that qualifies the word Date as the first step (Mod_Date, Created, etc.).  If for some reason the field believes it is a real date field, the all numeric text would work (since the text can be parsed to a number and internally dates are converted to numbers anyway), but any actual text that is not compatible with a true date parsing would fail.
PamFroemke
Deactivated User
Richard, thank you for your reply. I changed the field name to 'EDDI_Date'. Then I tested with fieldValue set to 'EDDI_ETrc_201300'. This did not work--the field remained blank. If I set fieldValue to '201300', then the field gets populated. It's like the field is not defined as a TEXT field.
0 Kudos
DanPatterson_Retired
MVP Emeritus
did you try str(fieldvalue) in your calculatefield expression?
0 Kudos
PamFroemke
Deactivated User
Thanks Dan. I did try that and got the same response--the field was left blank.
0 Kudos
DanPatterson_Retired
MVP Emeritus
just to be sure on something....the table is completely closed when you run your script, and when you open it, it is empty?
0 Kudos
PamFroemke
Deactivated User
Yes, that's correct, the tables are closed. I'm using Excel to view the dbf outputs, then closing them again before I run the script. ArcMap and ArcCatalog are also closed.
0 Kudos
DanPatterson_Retired
MVP Emeritus
try viewing the tables in ArcMap....also put "hello" in the calculatefield expression to rule out something else further up the line
0 Kudos
PamFroemke
Deactivated User
Dan, I can't even get that to work. My code line is:
arcpy.CalculateField_management(outTable, dateField, "hello")

If I try:
arcpy.CalculateField_management(outTable, dateField, "123")
the field is populated with 123.

I'm checking the tables with ArcMap, then closing it.
0 Kudos
PamFroemke
Deactivated User
Now I've tried:
fieldValue = chr(34) & os.path.splitext(part)[0] & chr(34)
and got an error, TypeError: unsupported operand type(s) for &: 'str' and 'str'.
0 Kudos