I need to compare fields from two separate tables and mark them with copy or no copy. It seems easy enough but i am having a hard time and not sure what I am doing wrong. I just get all "No" in the Copy field.
Here is what i have.
fc1 = 'C:\Temp\blah1.dbf'
fc2 = 'C:\Temp\blah2.dbf'
cursor1 = arcpy.da.SearchCursor(fc1, ["Field1", "Copy"])
cursor2 = arcpy.da.SearchCursor(fc2, ["Field2"])
with arcpy.da.UpdateCursor(fc1, ['Field1', 'Copy']) as cursor:
for row in cursor1:
for row2 in cursor2:
if row[0].strip().lower()!= row[0].strip().lower():
for row1 in cursor1:
row[1] = 'No'
else:
row[1] = 'Copy'
cursor.updateRow(row1)
del cursor
Solved! Go to Solution.
I'm not sure your list comprehension is working in your cursor. Check your list is being populated if not, replace with a noob append on fclist.
This list is working i add a print statement for the list. Not sure what part my code is failing.
fc1 = 'C:\Temp\blah1.dbf'
fc2 = 'C:\Temp\blah2.dbf'
fc1List = []
with arcpy.da.SearchCursor(fc2,["Field2"]) as cursor:
fc1List = [row for row in cursor]
print (fc1List)
tableFieldList = ["Field1", "Copy"]
with arcpy.da.UpdateCursor(fc1, tableFieldList) as tablecursor:
for tablerow in tablecursor:
if tablerow[0] in fc1List:
tablerow[1] = 'Copy'
else:
tablerow[1] = 'No'
tablecursor.updateRow(tablerow)
Looking at the code it seems absolutely fine. Is the comparison messing up on different data types, e.g. string and float?
Ran a test on my own data, could be what it is returning.
# ---- this returns tuples of values
with arcpy.da.SearchCursor(tbl1,["Text_vals"]) as cursor:
fc1List = [row for row in cursor]
fc1List
[('zilch',)]
# ---- This returns values
with arcpy.da.SearchCursor(tbl1,["Text_vals"]) as cursor:
fc1List = [row[0] for row in cursor]
fc1List
['zilch']
So you can't compare single values to tuples in case that is what you are getting.
Try seeing if the checks work before you go to the updatecursor
# tbl1, tbl2 are two tables with a common field (Text_vals)
with arcpy.da.SearchCursor(tbl1, ["Text_vals"]) as cursor:
fc1List = [row[0] for row in cursor]
print (fc1List)
tableFieldList = ['Text_vals'] # ---- field to check
with arcpy.da.SearchCursor(tbl2, tableFieldList) as tablecursor:
for tablerow in tablecursor:
if tablerow[0] in fc1List:
print("is in")
else:
print("not in")
# ---- print and run results
['zilch'.....
is in
....
Does something like this help:
fc1 = "C:\Temp\blah1.dbf"
fld1 = ["Field1", "Copy"]
fc2 = "C:\Temp\blah2.dbf"
fld2 = ["Field2"]
valueList = [r[0] for r in arcpy.da.SearchCursor(fc2, fld2)]
with arcpy.da.UpdateCursor(fc1, fld1) as cursor:
for row in cursor:
if row[0] in valueList:
row[1] = 'Copy' # May wish to swap 'Copy' and 'No'
else:
row[1] = 'No'
cursor.updateRow(row)
exactly my point..that is the slice that is needed. (my example, your example
fc1List = [row[0] for row in cursor]
valueList = [r[0] for r in arcpy.da.SearchCursor(fc2, fld2)]
If tuples are being returned, then it has to be sliced
'zilch' in [('zilch',)]
Out[1]: False
'zilch' in [('zilch',)[0]]
Out[2]: True
'zilch' in ['zilch']
Out[3]: True