Hi, I am trying to search through a dataset to find a certain value in a certain field in order to update another field in that same row, but I can't quite seem to make the code snippet work. Can somebody tell me what I'm doing wrong? The below code completes just fine but returns nothing.
import arcpy arcpy.env.workspace = r"C:\Users\paperspace\Google Drive\QTA\19010_PDX Zoning\PDXZoning\PDX_Kerns.gdb" fieldNames = ['TLID', 'RNO'] tlidList = arcpy.da.SearchCursor("taxlots_Kerns", fieldNames) for tlids in tlidList: string = "*-STR*" if tlids[1] == string: print(tlids)
I can't tell if I'm using the wildcard incorrectly, the comparison incorrectly, or if there's something else I'm doing wrong. Thanks,
Solved! Go to Solution.
Applying this logic to your code would be something like this.
import arcpy
arcpy.env.workspace = r"C:\Users\paperspace\Google Drive\QTA\19010_PDX Zoning\PDXZoning\PDX_Kerns.gdb"
fieldNames = ['TLID', 'RNO']
find_string = "-STR"
with arcpy.da.SearchCursor("taxlots_Kerns", fieldNames) as tlidList:
for tlid, rno in tlidList:
if find_string in tlid:
print(tlid)
Strings are iterables, so you can look for substrings in strings
s = "-STR"
tlids = [ "abc", "-str", "A_STR", "a_-STR"]
[s in i for i in tlids]
[False, False, False, True]
Applying this logic to your code would be something like this.
import arcpy
arcpy.env.workspace = r"C:\Users\paperspace\Google Drive\QTA\19010_PDX Zoning\PDXZoning\PDX_Kerns.gdb"
fieldNames = ['TLID', 'RNO']
find_string = "-STR"
with arcpy.da.SearchCursor("taxlots_Kerns", fieldNames) as tlidList:
for tlid, rno in tlidList:
if find_string in tlid:
print(tlid)
If you want to update rows, you should use an update cursor instead of a search cursor. Instead of doing the string search in Python, you can also put a SQL WHERE clause when you set up the cursor:
import arcpy
arcpy.env.workspace = r"C:\Users\paperspace\Google Drive\QTA\19010_PDX Zoning\PDXZoning\PDX_Kerns.gdb"
fieldNames = ['TLID', 'RNO']
tlidList = arcpy.da.SearchCursor("taxlots_Kerns", fieldNames)
sql = "RNO LIKE '%-STR%'"
with arcpy.da.UpdateCursor("taxlots_Kerns", fieldNames, sql) as cur:
for row in cur:
row.updateRow() # update row with new values