I have two fields: "Report_Number" and "Hyperlink." I want to take the first 11 characters from Report_Number and add them to Hyperlink while apending ".pdf" to the end. The result would be similar to: Report_Number = 20120328021; Hyperlink = 20120328021.pdf
So far, I have:
import arcpy
field = 'Report_Number'
field2 = 'Hyperlink'
cursor = arcpy.UpdateCursor("CrashReport")
for row in cursor:
row.setValue(field2, field[:11]) + '.pdf')
cursor.updateRow(row)
I am getting an error:
Runtime error
Traceback (most recent call last):
File "<string>", line 2, in <module>
File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\arcobjects\arcobjects.py", line 1044, in getValue
return convertArcObjectToPythonObject(self._arc_object.GetValue(*gp_fixargs(args)))
RuntimeError: ERROR 999999: Error executing function.
PLEASE HELP!
Solved! Go to Solution.
Bryce
i cannot see where you are assigning your values from your row to the fields you are using, try something like this:
import arcpy
fc = 'insert path to feature class'
fields = ('Report_Number', 'Hyperlink')
# Create update cursor for feature class
#
with arcpy.da.UpdateCursor(fc, fields) as cursor:
# For each row, evaluate the Report Number value (index position
# of 0), and update Hyperlink (index position of 1)
#
for row in cursor:
row[1] = row[0][:11] + ".pdf"
# Update the cursor with the updated list
#
cursor.updateRow(row)
Hi Bryce,
It looks like you have an extra ) on line 6. I don't think that's the issue but I'm not sure why it's there. Can you print the values of field and field 2 as it loops through the cursor and make sure the values make sense?
Thanks,
Brandon
I have also tried to simply print field by using the getValue command and I get the same error. It seems to have difficulty understanding the substring part. I can print just field, but when I try to just get the first 11 characters, it fails.
Hmmm...two questions. Have you tried replacing the single quote around your field names to double quotes? And does it work, if not the way you eventually want, if you remove the substring portion? As in row.setValue(field2, field + '.pdf')
Now it writes, "Report_Numb.pdf" to the Hyperlink field instead of the value from Report_Number field
x = row.getValue(field)
row.setValue(field2,x[:12]) + '.pdf')
splitting it into pieces and assuming x is a string I think...untested
Bryce
i cannot see where you are assigning your values from your row to the fields you are using, try something like this:
import arcpy
fc = 'insert path to feature class'
fields = ('Report_Number', 'Hyperlink')
# Create update cursor for feature class
#
with arcpy.da.UpdateCursor(fc, fields) as cursor:
# For each row, evaluate the Report Number value (index position
# of 0), and update Hyperlink (index position of 1)
#
for row in cursor:
row[1] = row[0][:11] + ".pdf"
# Update the cursor with the updated list
#
cursor.updateRow(row)
THANKS!!!
That works!
I have two fields: "Report_Number" and "Hyperlink." I want to take the first 11 characters from Report_Number and add them to Hyperlink while apending ".pdf" to the end. The result would be similar to: Report_Number = 20120328021; Hyperlink = 20120328021.pdf
Another solution would be to use the Calculate Field tool:
expr = "'{}.pdf'.format(!REPORT_NUMBER![:11])"
arcpy.CalculateField_management(tbl, "HYPERLINK", expr, "PYTHON")
Hi Bryce,
something that I found really helps in troubleshooting python when it runs embedded in other application, such as in the case of the ArcGIS environment, is winpdb (http://winpdb.org).
It might be a little overkill in your situation, but it will allow you to follow step by step what is happening within arcpy as it gets executed.
I've used it to debug add-ins as they run and it helped me immensely.
Cheers,
A