Creating a list that has strings not unicode

814
6
11-29-2022 02:01 PM
JacquelineAlessi
Occasional Contributor II

I am trying to use a search cursor to pull values as strings from a field and populate a list. Once the list is created, I need to loop through the list and pass the value be concatenated in a string that will then be passed to command line. 

In my screen shot below you can see that I tried to options.

Here is an example of my code: 

import arcpy
DeltaTable ='E:\\Data\\JRAlessi\\Conflation_URD_GPTest\\ConflationTest.gdb\\TestTable'
QTField = "QuarterTWNSHP"
cursor = arcpy.SearchCursor(DeltaTable, "FME_Status IS NULL")
QTSet = []
for row in cursor:
   row.getValue(str(QTField))
   QTset.append(str(row))

JacquelineAlessi_0-1669759677209.png

What am I doing wrong? when I go to print QTset I want to get back a true list that would look like this: 

['18644', '18643']

0 Kudos
6 Replies
DanPatterson
MVP Esteemed Contributor

Row—ArcGIS Pro | Documentation

don't str QTField or str row

try 

val = row.getValue(QTField)
# add val = str(val) if you really need it

and print should be print( ).

You are using python 3.x aren't you?


... sort of retired...
JacquelineAlessi
Occasional Contributor II

I'm kinda stuck with the version that I'm given. 

I tried this option and for whatever reason I am only getting a single return. Thank you. 

 

0 Kudos
BlakeTerhune
MVP Regular Contributor

Another option.

 

import arcpy
DeltaTable ='E:\\Data\\JRAlessi\\Conflation_URD_GPTest\\ConflationTest.gdb\\TestTable'
QTField = "QuarterTWNSHP"
QTSet = [str(row[0]) for row in arcpy.da.SearchCursor(DeltaTable, [QTField], "FME_Status IS NULL")]
print(QTSet)

 

0 Kudos
JacquelineAlessi
Occasional Contributor II

Hi I tried this option, and I am getting the following error: 

TypeError: 'Row' object does not support indexing

I changed the arcpy.searchcursor out for the arcpy.da.searchcursor and all appears to be working.

Thank you

0 Kudos
BlakeTerhune
MVP Regular Contributor

Sorry @JacquelineAlessi, I did not notice you were using the legacy cursor, as @Luke_Pinner pointed out. I updated my code to use the new data access cursor, but the post from Luke is more complete.

0 Kudos
Luke_Pinner
MVP Regular Contributor

Firstly, don't use the old legacy arcpy.SearchCursor, use the newer, faster arcpy.da.SearchCursor.

See Data access using cursors:


Legacy:

The data access module (arcpy.da) was added in ArcGIS 10.1. The original cursors are still supported; however, the new arcpy.da cursors include significantly faster performance. In most cases, the help documentation describes the use of the arcpy.da cursors. For more information about the classic cursor model, see the InsertCursor, SearchCursor, and UpdateCursor topics.


Second, you need to access the value from the row, not the row itself:

Data access cursor example:

 

with arcpy.da.SearchCursor(DeltaTable, [QTField], "FME_Status IS NULL") as cursor:
    QTSet = [row[0] for row in cursor]

 

Or row.getValue(QTField) if you really must use a legacy cursor...