Skip to main content
added 279 characters in body
Source Link
Bera
  • 77.7k
  • 14
  • 78
  • 187

It is difficult to debug your code without the data but some things you could try is:

Use AddFieldDelimiters and format to get correct syntax for all your sql queries, example:

feature_class = r'C:\data.gdb\features'
fieldname = 'Somefield'
sql = """{0} = 'NEW'""".format(arcpy.AddFieldDelimiters(datasource=feature_class, field=fieldname))
arcpy.TableSelect_analysis(in_table=feature_class, out_table='sometable', where_clause=sql)

I dont think you can pass a datetime object in an sql statment, you need to format it in a way arcpy will recognize. See this question: Using Python to select records by date field

To matchMatchinig two tables/featureclasses on two fields in each table can be done by combining the fields into another field, then join on this and use Field Calculator to update. Or use a dictionary and the da.UpdateCursor:

fc1 = r'C:\data.gdb\fc1'
fc1_keyfield1 = 'City'
fc1_keyfield2 = 'Street'
fc1_field_to_update = 'Olddata'

fc2 = r'C:\data.gdb\fc1'gdb\fc2'
fc2_keyfield1 = 'City'
fc2_keyfield2 = 'Street'
fc2_field_with_newdata = 'Newdata'

#Create a dictionary using the da.SearchCursor and dictionary comprehension
newvaluesdict = {i[0]+i[1]:i[2] for i in arcpy.da.SearchCursor(fc2,[fc2_keyfield1,fc2_keyfield2,fc2_field_with_newdata])}

with arcpy.da.UpdateCursor(fc1,[fc1_keyfield1,fc1_keyfield2,fc1_field_to_update]) as cursor:
    for row in cursor:
        if row[0]+row[1] in newvaluesdict:
            row[2] = newvaluesdict[row[0]+row[1]]
            cursor.updateRow(row)

In this example if you pass CityStreet to the dictionary it will return whatever value is in the Newdata field.:

some_data_returned_by_da_SearchCursor = [('Paris','Street1',123),('New York','Street2',456)]
d = {i[0]+i[1]:i[2] for i in some_data_returned_by_da_SearchCursor}

>>d
{'ParisStreet1': 123, 'New YorkStreet2': 456}

>>d['ParisStreet1']
123

It is difficult to debug your code without the data but some things you could try is:

Use AddFieldDelimiters and format to get correct syntax for all your sql queries, example:

feature_class = r'C:\data.gdb\features'
fieldname = 'Somefield'
sql = """{0} = 'NEW'""".format(arcpy.AddFieldDelimiters(datasource=feature_class, field=fieldname))
arcpy.TableSelect_analysis(in_table=feature_class, out_table='sometable', where_clause=sql)

I dont think you can pass a datetime object in an sql statment, you need to format it in a way arcpy will recognize. See this question: Using Python to select records by date field

To match two tables/featureclasses on two fields in each table can be done by combining the fields into another field, then join on this. Or use a dictionary and the da.UpdateCursor:

fc1 = r'C:\data.gdb\fc1'
fc1_keyfield1 = 'City'
fc1_keyfield2 = 'Street'
fc1_field_to_update = 'Olddata'

fc2 = r'C:\data.gdb\fc1'
fc2_keyfield1 = 'City'
fc2_keyfield2 = 'Street'
fc2_field_with_newdata = 'Newdata'

#Create a dictionary using the da.SearchCursor and dictionary comprehension
newvaluesdict = {i[0]+i[1]:i[2] for i in arcpy.da.SearchCursor(fc2,[fc2_keyfield1,fc2_keyfield2,fc2_field_with_newdata])}

with arcpy.da.UpdateCursor(fc1,[fc1_keyfield1,fc1_keyfield2,fc1_field_to_update]) as cursor:
    for row in cursor:
        if row[0]+row[1] in newvaluesdict:
            row[2] = newvaluesdict[row[0]+row[1]]
            cursor.updateRow(row)

In this example if you pass CityStreet to the dictionary it will return whatever value is in the Newdata field.

It is difficult to debug your code without the data but some things you could try is:

Use AddFieldDelimiters and format to get correct syntax for all your sql queries, example:

feature_class = r'C:\data.gdb\features'
fieldname = 'Somefield'
sql = """{0} = 'NEW'""".format(arcpy.AddFieldDelimiters(datasource=feature_class, field=fieldname))
arcpy.TableSelect_analysis(in_table=feature_class, out_table='sometable', where_clause=sql)

I dont think you can pass a datetime object in an sql statment, you need to format it in a way arcpy will recognize. See this question: Using Python to select records by date field

Matchinig two tables/featureclasses on two fields in each table can be done by combining the fields into another field, then join on this and use Field Calculator to update. Or use a dictionary and the da.UpdateCursor:

fc1 = r'C:\data.gdb\fc1'
fc1_keyfield1 = 'City'
fc1_keyfield2 = 'Street'
fc1_field_to_update = 'Olddata'

fc2 = r'C:\data.gdb\fc2'
fc2_keyfield1 = 'City'
fc2_keyfield2 = 'Street'
fc2_field_with_newdata = 'Newdata'

#Create a dictionary using the da.SearchCursor and dictionary comprehension
newvaluesdict = {i[0]+i[1]:i[2] for i in arcpy.da.SearchCursor(fc2,[fc2_keyfield1,fc2_keyfield2,fc2_field_with_newdata])}

with arcpy.da.UpdateCursor(fc1,[fc1_keyfield1,fc1_keyfield2,fc1_field_to_update]) as cursor:
    for row in cursor:
        if row[0]+row[1] in newvaluesdict:
            row[2] = newvaluesdict[row[0]+row[1]]
            cursor.updateRow(row)

In this example if you pass CityStreet to the dictionary it will return whatever value is in the Newdata field:

some_data_returned_by_da_SearchCursor = [('Paris','Street1',123),('New York','Street2',456)]
d = {i[0]+i[1]:i[2] for i in some_data_returned_by_da_SearchCursor}

>>d
{'ParisStreet1': 123, 'New YorkStreet2': 456}

>>d['ParisStreet1']
123
edited body
Source Link
Bera
  • 77.7k
  • 14
  • 78
  • 187

It is difficult to debug your code without the data but some things you could try is:

Use AddFieldDelimiters and format to get correct syntax for all your sql queries, example:

feature_class = r'C:\data.gdb\features'
fieldname = 'Somefield'
sql = """{0} = 'NEW'""".format(arcpy.AddFieldDelimiters(datasource=feature_class, field=fieldname))
arcpy.TableSelect_analysis(in_table=feature_class, out_table='sometable', where_clause=sql)

I dont think you can pass a datetime object in an sql statment, you need to format it in a way arcpy will recognize. See this question: Using Python to select records by date field

To match two tables/featureclasses on two fields in each table can be done by combining the fields into another field, then join on this. Or use a dictionary and the da.UpdateCursor:

fc1 = r'C:\data.gdb\fc1'
fc1_keyfield1 = 'City'
fc1_keyfield2 = 'Street'
fc1_field_to_update = 'Olddata'

fc2 = r'C:\data.gdb\fc1'
fc2_keyfield1 = 'City'
fc2_keyfield2 = 'Street'
fc2_field_with_newdata = 'Olddata''Newdata'

#Create a dictionary using the da.SearchCursor and dictionary comprehension
newvaluesdict = {i[0]+i[1]:i[2] for i in arcpy.da.SearchCursor(fc2,[fc2_keyfield1,fc2_keyfield2,fc2_field_with_newdata])}

with arcpy.da.UpdateCursor(fc1,[fc1_keyfield1,fc1_keyfield2,fc1_field_to_update]) as cursor:
    for row in cursor:
        if row[0]+row[1] in newvaluesdict:
            row[2] = newvaluesdict[row[0]+row[1]]
            cursor.updateRow(row)

In this example if you pass CityStreet to the dictionary it will return whatever value is in the Newdata field.

It is difficult to debug your code without the data but some things you could try is:

Use AddFieldDelimiters and format to get correct syntax for all your sql queries, example:

feature_class = r'C:\data.gdb\features'
fieldname = 'Somefield'
sql = """{0} = 'NEW'""".format(arcpy.AddFieldDelimiters(datasource=feature_class, field=fieldname))
arcpy.TableSelect_analysis(in_table=feature_class, out_table='sometable', where_clause=sql)

I dont think you can pass a datetime object in an sql statment, you need to format it in a way arcpy will recognize. See this question: Using Python to select records by date field

To match two tables/featureclasses on two fields in each table can be done by combining the fields into another field, then join on this. Or use a dictionary and the da.UpdateCursor:

fc1 = r'C:\data.gdb\fc1'
fc1_keyfield1 = 'City'
fc1_keyfield2 = 'Street'
fc1_field_to_update = 'Olddata'

fc2 = r'C:\data.gdb\fc1'
fc2_keyfield1 = 'City'
fc2_keyfield2 = 'Street'
fc2_field_with_newdata = 'Olddata'
#Create a dictionary using the da.SearchCursor
newvaluesdict = {i[0]+i[1]:i[2] for i in arcpy.da.SearchCursor(fc2,[fc2_keyfield1,fc2_keyfield2,fc2_field_with_newdata])}

with arcpy.da.UpdateCursor(fc1,[fc1_keyfield1,fc1_keyfield2,fc1_field_to_update]) as cursor:
    for row in cursor:
        if row[0]+row[1] in newvaluesdict:
            row[2] = newvaluesdict[row[0]+row[1]]
            cursor.updateRow(row)

It is difficult to debug your code without the data but some things you could try is:

Use AddFieldDelimiters and format to get correct syntax for all your sql queries, example:

feature_class = r'C:\data.gdb\features'
fieldname = 'Somefield'
sql = """{0} = 'NEW'""".format(arcpy.AddFieldDelimiters(datasource=feature_class, field=fieldname))
arcpy.TableSelect_analysis(in_table=feature_class, out_table='sometable', where_clause=sql)

I dont think you can pass a datetime object in an sql statment, you need to format it in a way arcpy will recognize. See this question: Using Python to select records by date field

To match two tables/featureclasses on two fields in each table can be done by combining the fields into another field, then join on this. Or use a dictionary and the da.UpdateCursor:

fc1 = r'C:\data.gdb\fc1'
fc1_keyfield1 = 'City'
fc1_keyfield2 = 'Street'
fc1_field_to_update = 'Olddata'

fc2 = r'C:\data.gdb\fc1'
fc2_keyfield1 = 'City'
fc2_keyfield2 = 'Street'
fc2_field_with_newdata = 'Newdata'

#Create a dictionary using the da.SearchCursor and dictionary comprehension
newvaluesdict = {i[0]+i[1]:i[2] for i in arcpy.da.SearchCursor(fc2,[fc2_keyfield1,fc2_keyfield2,fc2_field_with_newdata])}

with arcpy.da.UpdateCursor(fc1,[fc1_keyfield1,fc1_keyfield2,fc1_field_to_update]) as cursor:
    for row in cursor:
        if row[0]+row[1] in newvaluesdict:
            row[2] = newvaluesdict[row[0]+row[1]]
            cursor.updateRow(row)

In this example if you pass CityStreet to the dictionary it will return whatever value is in the Newdata field.

Source Link
Bera
  • 77.7k
  • 14
  • 78
  • 187

It is difficult to debug your code without the data but some things you could try is:

Use AddFieldDelimiters and format to get correct syntax for all your sql queries, example:

feature_class = r'C:\data.gdb\features'
fieldname = 'Somefield'
sql = """{0} = 'NEW'""".format(arcpy.AddFieldDelimiters(datasource=feature_class, field=fieldname))
arcpy.TableSelect_analysis(in_table=feature_class, out_table='sometable', where_clause=sql)

I dont think you can pass a datetime object in an sql statment, you need to format it in a way arcpy will recognize. See this question: Using Python to select records by date field

To match two tables/featureclasses on two fields in each table can be done by combining the fields into another field, then join on this. Or use a dictionary and the da.UpdateCursor:

fc1 = r'C:\data.gdb\fc1'
fc1_keyfield1 = 'City'
fc1_keyfield2 = 'Street'
fc1_field_to_update = 'Olddata'

fc2 = r'C:\data.gdb\fc1'
fc2_keyfield1 = 'City'
fc2_keyfield2 = 'Street'
fc2_field_with_newdata = 'Olddata'
#Create a dictionary using the da.SearchCursor
newvaluesdict = {i[0]+i[1]:i[2] for i in arcpy.da.SearchCursor(fc2,[fc2_keyfield1,fc2_keyfield2,fc2_field_with_newdata])}

with arcpy.da.UpdateCursor(fc1,[fc1_keyfield1,fc1_keyfield2,fc1_field_to_update]) as cursor:
    for row in cursor:
        if row[0]+row[1] in newvaluesdict:
            row[2] = newvaluesdict[row[0]+row[1]]
            cursor.updateRow(row)