Select to view content in your preferred language

Using PyWin to take the first 11 characters from a field and apend ".pdf" to the end in a new field

2090
9
Jump to solution
10-23-2014 11:32 AM
BryceGardner1
Emerging Contributor

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!

0 Kudos
1 Solution

Accepted Solutions
AnthonyGiles
Honored Contributor

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)

View solution in original post

9 Replies
BrandonKeinath1
Deactivated User

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

0 Kudos
BryceGardner1
Emerging Contributor

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.

0 Kudos
BrandonKeinath1
Deactivated User

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')

0 Kudos
BryceGardner1
Emerging Contributor

Now it writes, "Report_Numb.pdf" to the Hyperlink field instead of the value from Report_Number field

0 Kudos
DanPatterson_Retired
MVP Emeritus

x = row.getValue(field)

row.setValue(field2,x[:12]) + '.pdf')

splitting it into pieces and assuming x is a string I think...untested

AnthonyGiles
Honored Contributor

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)

BryceGardner1
Emerging Contributor

THANKS!!!

That works!

0 Kudos
curtvprice
MVP Esteemed Contributor

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")

0 Kudos
AndreaVaccari
Deactivated User

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

0 Kudos