case_number = arcpy.GetParameterAsText(0) # user enters Z-FY-12-34 . . . fc = "Cases" fld = "Case_" arcpy.AddMessage("Adding Case number " + case_number + " added.") # case_number = Z-FY-12-34 try: arcpy.CalculateField_management(fc, fld, case_number) # field value set at -124 or some other number arcpy.AddMessage("Case number " + case_number + " added.") # case_number = Z-FY-12-34 except Exception as e: arcpy.AddError("Error setting Case_ field value to case number. \n" + e.message)
Solved! Go to Solution.
arcpy.CalculateField_management(fc, fld, '"'+case_number+'"') #Or arcpy.CalculateField_management(fc, fld, '"%s"' % case_number)
arcpy.CalculateField_management(fc, fld, '"'+case_number+'"') #Or arcpy.CalculateField_management(fc, fld, '"%s"' % case_number)
Did you ever figure this out? I am trying to use arcpy.CalculateField_management to add the filepath and filename to a field and if the folder begins with a number, it distorts the name. For example a portion of the file path might look like:
...\Date\Client\180705\1_Event\Point.shp
will look like:
...\Date\Client 180705 @_Event\Point.shp
I will want do calculations in other fields based on these values using the .split("\") function, so it's important that I get the complete, correct filepath.
Did you ever figure this out?
The answer is to quote the strings in the expression.
If you're having trouble, put some more detail (i.e some code would be good) in about your input values and how you construct the expression.
Also, don't parse paths with .split("\")... Use os.path.split(your_file_path) which will handle all the slash permutations for you.
Here is what the code looks like (not sure how to add to this as a code sample, just as text.) I've also tried adding an "r" in various forms in the CalculateField line, to no avail. When using the print function for the fc, the filepath/name appears as it should.
The parsing with the .split("\") was for a later field calculator expression, but mentioned because to indicate that it is important that the file path be exact.
for dirpath, dirnames, filenames in walk:
for filename in filenames:
fcs.append(os.path.join(dirpath, filename))
for fc in fcs:
print fc
arcpy.CalculateField_management(fc, 'Source', '"' + fc + '"' , "PYTHON")
Try
arcpy.CalculateField_management(fc, 'Source', 'r"' + fc + '"' , "PYTHON")
that. was. it.
Thank you, I was pounding my head against the wall on that one!
What you (and lots of others) were probably struggling with is you need to construct a valid python expression inside a python string, not as an actual python expression.
For example, say you wanted to calculate a string field to be something like r"A:\B\D.d", if you passed that to arcpy.CalculateField_management, the tool would only see A:\B\D.d which is not valid python (it's not quoted) and the backslashes wouldn't be escaped. You would construct your expression as r'r"A:\B\D.d"'. There's a few ways to make this easier to get your head around with string formatting.
In simple terms, think about what you need to have valid python syntax, then wrap all that in quotes to make a string.
string split with backslashes need to be escaped ie "\\"
the differences are as in the example
win_path = 'C:\\GIS\\A_Tools_scripts\\Polygon_lineTools\\Scripts\\arcpytools_plt.py'
win_path.split("\\")
['C:',
'GIS',
'A_Tools_scripts',
'Polygon_lineTools',
'Scripts',
'arcpytools_plt.py']
os.path.split(win_path)
('C:\\GIS\\A_Tools_scripts\\Polygon_lineTools\\Scripts', 'arcpytools_plt.py')