Strange errors in Pro's Python window with search cursor

569
11
Jump to solution
07-17-2020 03:30 PM
DanDeegan
New Contributor III
address_sum = 0
with arcpy.da.SearchCursor('AddressPtsFormatted' ["PLOC_"]) as cursor:
   for r in cursor:
   number = r[0]
   address_sum += number‍‍‍‍

returns the error: 

Traceback (most recent call last):File "<string>", line 1, in <module> TypeError: string indices must be integers‍‍‍‍‍‍‍

The code runs in jupytr notebook

(no, i wasn't adding up every address in my area because I am loopy on Friday afternoon, why would anyone find the sum of addresses)

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

At a minimum, you are missing a comma after the first argument in your SearchCursor.  Try fixing that first.

View solution in original post

0 Kudos
11 Replies
JoshuaBixby
MVP Esteemed Contributor

It looks like you posted a partial Traceback, can you share the whole thing?  Does Line 1 in your code snippet line up with Line 1 in the error message?  I am guessing not because setting a variable to zero would not generate a TypeError.

0 Kudos
DanDeegan
New Contributor III

That was what was in the Python window in Pro. 

Normally when I used cursors, row[0] is not an issue

and it works in this:

PLOCU_set= set(r[0].replace(" ", "") for r in arcpy.da.SearchCursor(fc, ["PLOCU_"]) if not r[0] is None)
0 Kudos
DanPatterson
MVP Esteemed Contributor

also, lines 4 and 5 are not indented properly


... sort of retired...
0 Kudos
DanDeegan
New Contributor III

I tried to make it look better from what was pasted in from Pro. The code runs fine in a jupytr notebook.

0 Kudos
DanDeegan
New Contributor III
fc = "AddressPtsFormatted"
address_sum = 0
with arcpy.da.SearchCursor('AddressPtsFormatted' ["PLOC_"]) as cursor:
   for r in cursor:
       number = r[0]
       address_sum += number
Traceback (most recent call last):
  File "<string>", line 3, in <module>
TypeError: string indices must be integers‍‍‍‍‍‍‍‍‍

This is what I ran. I cant edit the first post. And I just learned that it helps to set the language before you past code

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

At a minimum, you are missing a comma after the first argument in your SearchCursor.  Try fixing that first.

0 Kudos
DanDeegan
New Contributor III

ohhhhhhhhhhhh thats it i bet

0 Kudos
curtvprice
MVP Esteemed Contributor

I cant edit the first post.

Look for the edit button at upper right (you can't edit in a dropdown but you can edit by clicking up there).

Not that it matters that much: new Geonet platform soon!

0 Kudos
DanDeegan
New Contributor III

Side by Side comparison of jupytr notebook and Pro

0 Kudos