Select to view content in your preferred language

Python script (ArcMap 10.8 - SDE DB - Feature class versioned) -created version, joined attribute to another feature class and using field calculator

497
0
09-11-2023 07:25 AM
Labels (1)
SailajaYarlagadda
New Contributor

Python script (ArcMap 10.8 - SDE DB - Feature class versioned) -

Below is the python script

import arcpy
 
arcpy.env.workspace = r"Database Connections\XXXXX.sde"
arcpy.env.overwriteOutput = True
# Print to the Interactive window all the feature datasets in the
#   workspace that start with the letter C.
datasetList = arcpy.ListDatasets("*", "Feature")
for dataset in datasetList:
    #print dataset
    fcList = arcpy.ListFeatureClasses("*","",dataset)
    for fc in fcList:
        #print fc
            fcl = "XXXXX"
if arcpy.Exists(fcl):
print("exists")
else:
    print("not exists")
db = arcpy.env.workspace    
versions = arcpy.ListVersions(db)
makelayerName = "espole1"
selectionlayerName = "espoleselection1"
#print versions
#-------------------------------------------------#
#for versions in arcpy.da.ListVersions(db):
 
#if 'ELECTRIC.STPOLEtest' in version.name:
#print("Version Exists")
 
#versionname = 'ELECTRIC.STPOLEtest'
        #arcpy.MakeTableView_management(fcl, "fcl_view", "", "", "")
 
        #whereClause = """"{} = '{}'""".format(arcpy.AddFieldDelimiters(datasource = fcl, field="FIXTURE"), 'Bell')
#yr = arcpy.MakeFeatureLayer_management("esPole_vw","espole","FIXTURE ='Cobra'")
lyr1 = arcpy.MakeFeatureLayer_management("esPole",makelayerName)
#arcpy.CopyFeatures_management("espole1", "C:/output/output.gdb/climate")
arcpy.SelectLayerByAttribute_management(makelayerName,"NEW_SELECTION","""FIXTURE = 'Single Pendant'""")
#copylyr1 = arcpy.CopyFeatures_management("espole3", "espoleselection1")
lyrselection = arcpy.MakeFeatureLayer_management(makelayerName,selectionlayerName )
#lyr = arcpy.MakeFeatureLayer_management("esPole_vw","espole")
print("Feature Layer created")  
result1 = arcpy.GetCount_management(lyrselection)
print('{} has {} records'.format(fcl, result1[0]))
arcpy.ChangeVersion_management(selectionlayerName,'Transactional','ELECTRIC.STPOLEtest','')
print("Version Changed")
#-------------------------Joining attributes
inFeatures = "feature class"
layerName = "espole1"
inField = "attribute"
joinTable = "Table to join"
joinField = "attribute to join"
arcpy.AddJoin_management (selectionlayerName, inField, joinTable, joinField)
print "Join Completed"
#--------------------------Joining Attributes Tested until here
edit = arcpy.da.Editor(db)
edit.startEditing(True)
#arcpy.AddMessage(isEditing)
edit.startOperation()
print "started editing"
#-------------------------------
listfield = arcpy.ListFields(fcl)
for field in listfield:
    #if ((field.name == "FIXTURE") or (field.name == "Fixture")):
    #    print("Fixture exists")
    if ((field.name == "GLOBALID_1")):
        print "GLOBALID_1 Field exists"
        fieldname = "GLOBALID_1"
        #delimfield = arcpy.AddFieldDelimiters(fcl, fieldname)
        expression = u'{} = GLOBALID_1'.format(arcpy.AddFieldDelimiters(fc, fieldname))
        #whereClause = """{} = Bell""".format(arcpy.AddFieldDelimiters(fcl, fieldname))
        cursor = arcpy.da.SearchCursor(fcl, fieldname, expression)
        for row in cursor:
            if row[0] =='Global ID value':
                print(row[0])
                print "working"
                for fc1 in fcList:
                    #print fc
                    fcl1 = "FISWEB.Streetlightstaging"
                    listfield1 = arcpy.ListFields(fcl1)
                    for field1 in listfield1:
                        if ((field1.name == "GUID")):
                            #print "GUID Field exists"
                            #fieldname1 = 'Global ID value'
                            fieldname1 = "GUID"
                            #print fieldname1
                            #delimfield = arcpy.AddFieldDelimiters(fcl, fieldname)
                            expression1 = u'{} = MLGWGUID'.format(arcpy.AddFieldDelimiters(fc1, fieldname1))
                            #whereClause = """{} = Bell""".format(arcpy.AddFieldDelimiters(fcl, fieldname))
                            cursor1 = arcpy.da.SearchCursor(fcl1, fieldname1, expression1)
                            for row1 in cursor1:
                                if row1[0] == 'GlobalId value':
                                    if row1[0] ==row[0]:
                                        print("same")
                                        fieldname2 = "FIXTURE_INSTALLED"
                                        expression2 = u'{} = FIXTURE_INSTALLED'.format(arcpy.AddFieldDelimiters(fc1, fieldname2))
                                        #whereClause = """{} = Bell""".format(arcpy.AddFieldDelimiters(fcl, fieldname))
                                        cursor2 = arcpy.da.SearchCursor(fcl1, fieldname2, expression2)
                                        for row2 in cursor2:
                                            #row2.getValue(fieldname2)
                                            if ((fieldname2 == "YES") & (row1[0] == 'GlobalID value)) :
                                                rowCount = 0
                                                for row3 in cursor2:
                                                    rowCount = rowCount + 1
                                        #print rowCount
                                        print('{} has {} records'.format(fcl1, rowCount)) 
                                        # ------------------- Everything works fine until here Working on calc
                                        calcfieldName = "DHEditor"
                                        expression ="test"
                                        # #expression = '"!EDITOR!"'
                                        print expression
                                        arcpy.CalculateField_management(selectionlayerName, calcfieldName, expression, "PYTHON_9.3")  
                                        #-------------------------------
#------------------------------------
edit.stopOperation()
edit.stopEditing(True)
print "stopped editing"
#arcpy.ChangeVersion_management('espole','Transactional','SDE.Default','')   
 
I am getting the error at below line
arcpy.CalculateField_management(selectionlayerName, calcfieldName, expression, "PYTHON_9.3")
 
SailajaYarlagadda_0-1694441030315.png

 

       
0 Kudos
0 Replies