AnsweredAssumed Answered

da.SearchCursor: where_clause is not respected

Question asked by n.ueberschaeresri-rw-esridist Employee on Oct 5, 2017
Latest reply on Oct 6, 2017 by n.ueberschaeresri-rw-esridist

Hello, 

 

I'm still working on my profiles with points etc. 

Now I am facing a problem with my da.SearchCursor. While it was working well with my original tables, it is not respecting my where_clause any longer since I reduced the tables to the attributes I actually need. 

 

def store_table(table):
    arcpy.AddMessage("open "+table)

    #get field names
    desc = arcpy.Describe(table)
    fieldnames=[field.name.encode("utf-8") for field in desc.fields]

    #get profile numbers
    profile_nrs=unique_values(table, "Profile No")
    arcpy.AddMessage(profile_nrs)

    tablefile=location_for_tables+"\\"+table #modified table
    # Select rows by station nr and save to txt file
    for profile in profile_nrs:
        if profile is not None and profile!=0:
            expression="'Profile No'='"+str(profile)+"'"

            arcpy.AddMessage(expression)
            with arcpy.da.SearchCursor(table,"*",where_clause=expression) as sCursor:
                for sRow in sCursor:
                    table_name=str(sRow[2])+"_"+str(sRow[3])+"_.txt" #Campaign + Profile_No
                    arcpy.AddMessage("table_name="+table_name)
                    filepath=DirOutTables +"\\"+ table_name   #seperated tables
                    UIDvalue=str(sRow[2])+"_"+str(sRow[3])
                    UIDGraphValue=UIDvalue+"_"
                    break
                if arcpy.Exists(filepath):
                    arcpy.AddMessage("Table "+table_name+" exists")
                else:
                    with open(tablefile, 'r') as t:
                        with open(filepath, 'a') as f:
                            w = csv.writer(f, delimiter="\t", lineterminator='\n')
                            r = csv.reader(t, delimiter="\t")
                            allnewtable=[]

                            theader=r.next()
                            theader.append('UID')
                            theader.append('UIDGraph')
                            allnewtable.append(theader)
                            arcpy.AddMessage(theader)

                            for sRow in sCursor:
                                sRowNew=sRow + (UIDvalue,UIDGraphValue)
                                allnewtable.append(sRowNew)
                            w.writerows(allnewtable)

                    del sRow
                    print("Table "+table_name+" created")
                    arcpy.AddMessage("Table "+table_name+" created")
        else:
            print("empty line")

 

In my test file I have 4 profiles in profile_nrs and with each loop through "for profile in profile_nrs" the expression is iterated correctly. But obviously the where_clause is not respected because sRow[3] is always 1. Also when I hard-code the expression with where_clause="'Profile_No'='4'" it still gives me back the whole table.  

 

I thought maybe I used the sCursor already in another part of the script but that's not the case.

I tried the expression without putting the number in quotation marks but then it's not a valid sql expression it says. 

 

There is no error message and the script runs through up to the end, with the only mistake that it creates only one table instead of four. 

 

Do you have an idea what goes wrong?

 

Thanks in advance!

Outcomes