Select to view content in your preferred language

Comparing Fields in gdb and shapefile, using Search Cursor List!

1121
5
04-16-2013 07:28 AM
MichelleCouden1
Deactivated User
I need some guidance. I am writing a program to search in a field from a shapefile and search in a field in a gdb. I am looking for a value in the gdb that is equal to the value in the shapefile. For Example: If field T_FLAG is 10H59 then it needs to go find that equal in the gdb field TFLAG 10H59. I have started a program I just need to know if I am headed in the right direction or am I way off. Thanks!!


import arcpy, os, sys, string
arcpy.env.overwriteOutput = True
fc = "K:\TASS\4_MAPPING_DATA_SUPPORT\Traffic_Mapping\Traffic_Count_Data\2011_Counts\2011_Annual_Stations\Final_Annual_Stations_2012.shp"
fields = ("TFLAG", "T_FLAG")
Value = "TFLAG" <> "T_FLAG"
where = "%s = '%s'" % (field, vlaue)
dhList = []
for w in ws:
Tags (2)
0 Kudos
5 Replies
MichelleCouden1
Deactivated User
Here I've added some more:

import arcpy, os, sys, string
arcpy.env.overwriteOutput = True
fc = "K:\TASS\4_MAPPING_DATA_SUPPORT\Traffic_Mapping\Traffic_Count_Data\2011_Counts\2011_Annual_Stations\Final_Annual_Stations_2012.shp"
fields = ("TFLAG", "T_FLAG")
Value = "TFLAG" <> "T_FLAG"
where = "%s = '%s'" % (field, vlaue)
dhList = []
for w in ws:
    arcpy.env.workspace = w
    gdb = arcpy.ListWorkspaces ("*", "TFLAG")
    for fc in gdb:
        arcpy.env.workspace = fc


0 Kudos
MichelleCouden1
Deactivated User
Better Explanation!!
I need help!! I am writing a program where inside the mxd I have two layers. One layer is a shapefile and one layer is the annotation of same shapefile. The program is supposed to find missing annotation in the annotation layer. It's easy because if the annotation is missing there is no data in the attribute table. For example, the shapefile has the field TFlag that has the station id in it and the annotation has the field TFlag that is supposed to have the same id. So, I was going to write the program to look for the same station id in the annotation layer that the station layer has and when it can't find it highlight it. I have written some of the program I need to know if I'm heading in the right direction or am I way off?
0 Kudos
MelanieSummers
Frequent Contributor
Hi Michelle,

This worked for me, it does assume that all values being compared are unique though.

import arcpy

flayer = "Points"
alayer = "PointsAnno"

FRows = arcpy.SearchCursor(flayer,"", sort_fields="ID A")
ARows = arcpy.SearchCursor(alayer,"", sort_fields="TextString A")

FList = []
AList = []

for row in FRows:
 Fvalue = row.getValue("ID")
 FList.append(str(Fvalue))

for row in ARows:
 Avalue = row.getValue("TextString")
 AList.append(str(Avalue))
 
matched = set(FList) & set(AList)

for X in matched:
 exp = "ID = " + X
 arcpy.SelectLayerByAttribute_management(flayer, "ADD_TO_SELECTION", exp)
arcpy.SelectLayerByAttribute_management(flayer, "SWITCH_SELECTION")


Best,
Melanie S.
0 Kudos
MichelleCouden1
Deactivated User
Running my code in Python window inside ArcMAP I get a Runtime error. It says "Search Cursor() got an unexpected keyword argument 'sort_fields'" I'm not understanding that because the fields FLAG and TFLAG are the fields in the layer it has to go through.

import arcpy, os, sys, string

flayer = "AADT"
alayer = "AADT AnnoLabel"

FRows = arcpy.SearchCursor(flayer,"", sort_fields="FLAG")
ARows = arcpy.SearchCursor(alayer,"", sort_fields="TFLAG")

FList = []
AList = []

for row in FRows:
    Fvalue = row.getValue("ID")
    FList.append(str(Fvalue))

for rows in ARows:
    Avalue = row.getValue("TextString")
    AList.append(str(Avalue))

matched = set(FList) & set(AList)

for x in matched:
    exp = "ID = " + x
    arcpy.SelectLayerByAttribute_management(flayer, "ADD_TO_SELECTION", exp)
    arcpy.SelectLayerByAttribute_management(flayer, "SWTCH_SELECTION")
Runtime error <type 'exceptions.TypeError'>: SearchCursor() got an unexpected keyword argument 'sort_fields'
>>> 

0 Kudos
MichelleCouden1
Deactivated User
I've corrected a few errors I found when I gave it a rest and went back to it. But I am still getting an error on the sort fields.  "Search Cursor() got an unexpected keyword argument 'sort_fields'". Could someone please explain that to me. This code is what I need though because it searches the layers in the mxd which is best.

import arcpy

flayer = "AADT"
alayer = "AADTAnnoLabel"

FRows = arcpy.SearchCursor(flayer,"", sort_fields="FLAG A")
ARows = arcpy.SearchCursor(alayer,"", sort_fields="TFLAG A")

FList = []
AList = []

for row in FRows:
    Fvalue = row.getValue("FLAG")
    FList.append(str(Fvalue))

for rows in ARows:
    Avalue = row.getValue("TFLAG")
    AList.append(str(Avalue))

matched = set(FList) & set(AList)

for x in matched:
    exp = "ID = " + x
    arcpy.SelectLayerByAttribute_management(flayer, "ADD_TO_SELECTION", exp)
    arcpy.SelectLayerByAttribute_management(flayer, "SWTCH_SELECTION")

0 Kudos