Varianble Name function

4419
10
Jump to solution
09-24-2014 11:54 PM
MittRamgobin
New Contributor

Hi,

 

 

Is there a function that returns the name of the variable rather than its contents.

 

For example.

 

T = 1

R= 2

S= 3

 

P = (T, R, S)

Is there a fucntion that if I do function(T) it will return T?

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor

Please see Posting Code blocks in the new GeoNet

Suggested change, use a list, and create a list of labels in the same order:


Transmission = [T330OH, T330UG, T220OV, T132OH, T132UG, T66OV, T66UG, T33OH, T33UG, TransStruc, Substation]


TransLabels = ["T330OH", "T330UG", "T220OV", "T132OH"  (etc)




Then at the end:


k = 0


for Struc in Transmission:
    outputPath = r"S:\SupportServices\LGAShapefiles"
    basefileName = str(LGA2)+str(Struc))
    fileFormat= '.shp'
    finalDest = os.path.join(outputPath,basefileName+Translabels + fileFormat)
    arcpy.Clip_analysis(Struc,"LGAlyr", finalDest)


    k += 1


print "Process ended"




An unrelated comment, you seem to be using ListFeatureClasses (which returns a list) when you really want a path to a single dataset:

maybe instead of

T330OH = arcpy.ListFeatureClasses("GISTRAN.Trans_330kV_OH_Carrier","All","GISTRAN.Transmission")

you mean:

T330OH = os.path.join("GISTRAN.Transmission", "GISTRAN.Trans_330kV_OH_Carrier")

View solution in original post

0 Kudos
10 Replies
curtvprice
MVP Esteemed Contributor

Can you explain what you are trying to do?

What do you do after P = (T,R,S)?

P[0] and P[T-1] will both return 1.

eval("T") will also return 1.

0 Kudos
MittRamgobin
New Contributor


I want to loop through P and then return T R S, not 1 2 3

e.g

for x in P

filename = " XYZ " + T

The desired output is XYZT not XYZ1

Is that possible?

0 Kudos
curtvprice
MVP Esteemed Contributor

You could create another list:

P1 = ["T","R","S"]

filename = "XYZ" + P1[0]

Or maybe a dictionary?

D = {1:"T", 2: "R", 3: "S"}

D[1] is "T"

if T == 1, D is "T"

0 Kudos
MittRamgobin
New Contributor

import arcpy
import sys
import os

# Script to create LGA Map with WP Infrastructure

# Define variables

arcpy.env.workspace = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde"
arcpy.env.overwriteOutput = True
#LGA = arcpy.GetParameterAsText(0) # Input Required Local Government Area
LGA ="COOROW, SHIRE OF"
LGA2 = "COOROW"

#Define Layers to Intersect

#1. Transmission Layers
T330OH = arcpy.ListFeatureClasses("GISTRAN.Trans_330kV_OH_Carrier","All","GISTRAN.Transmission")
T330UG = arcpy.ListFeatureClasses("GISTRAN.Trans_330kV_UG_Cable","All","GISTRAN.Transmission")
T220OV = arcpy.ListFeatureClasses("GISTRAN.Trans_220kV_OH_Carrier","All","GISTRAN.Transmission")
T132OH = r"C:\Users\N041871\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\DynamicCredentials@GISR.sde\GISTRAN.Transmission\GISTRAN.Trans_132kV_OH_Carrier"
T132UG = arcpy.ListFeatureClasses("GISTRAN.Trans_132kV_UG_Cable","All","GISTRAN.Transmission")
T66OV = arcpy.ListFeatureClasses("GISTRAN.Trans_66kV_OH_Carrier","All","GISTRAN.Transmission")
T66UG = arcpy.ListFeatureClasses("GISTRAN.Trans_66kV_UG_Cable","All","GISTRAN.Transmission")
T33OH = arcpy.ListFeatureClasses("GISTRAN.Trans_33kV_OH_Carrier","All","GISTRAN.Transmission")
T33UG = arcpy.ListFeatureClasses("GISTRAN.Trans_33kV_UG_Cable","All","GISTRAN.Transmission")
TransStruc = arcpy.ListFeatureClasses("GISTRAN.Trans_Structure","All","GISTRAN.Transmission")
Substation = arcpy.ListFeatureClasses("GISTRAN.Substation","All","GISTRAN.Transmission")
#Group Transmission Values:
Transmission = (T330OH, T330UG, T220OV, T132OH, T132UG, T66OV, T66UG, T33OH, T33UG, TransStruc, Substation)

                               
#Select Required LGA
arcpy.MakeFeatureLayer_management("GISEXT.Local_Government_Authority", "LGAlyr")
fieldName = "NAME"
Query = "\""+fieldName+"\"='"+LGA+"'"
arcpy.SelectLayerByAttribute_management ("LGAlyr", "NEW_SELECTION", Query)
###arcpy.SelectLayerByAttribute_management ("LGAlyr", "NEW_SELECTION", """ "NAME" = 'ALBANY, CITY OF' """) - working code

for Struc in Transmission:
outputPath = r"S:\SupportServices\LGAShapefiles"
basefileName = str(LGA2)+str(Struc))
fileFormat= '.shp'
finalDest = os.path.join(outputPath,basefileName+fileFormat)
#print Struc

arcpy.Clip_analysis(T132OH, "LGAlyr",finalDest)
#arcpy.Clip_analysis(Struc,"LGAlyr", finalDest)

print "Process ended"

That is my code.

In the last loop I want to use the variable name to name the output file and the variable content for geoprocessing....

Is that possible?

0 Kudos
curtvprice
MVP Esteemed Contributor

Please see Posting Code blocks in the new GeoNet

Suggested change, use a list, and create a list of labels in the same order:


Transmission = [T330OH, T330UG, T220OV, T132OH, T132UG, T66OV, T66UG, T33OH, T33UG, TransStruc, Substation]


TransLabels = ["T330OH", "T330UG", "T220OV", "T132OH"  (etc)




Then at the end:


k = 0


for Struc in Transmission:
    outputPath = r"S:\SupportServices\LGAShapefiles"
    basefileName = str(LGA2)+str(Struc))
    fileFormat= '.shp'
    finalDest = os.path.join(outputPath,basefileName+Translabels + fileFormat)
    arcpy.Clip_analysis(Struc,"LGAlyr", finalDest)


    k += 1


print "Process ended"




An unrelated comment, you seem to be using ListFeatureClasses (which returns a list) when you really want a path to a single dataset:

maybe instead of

T330OH = arcpy.ListFeatureClasses("GISTRAN.Trans_330kV_OH_Carrier","All","GISTRAN.Transmission")

you mean:

T330OH = os.path.join("GISTRAN.Transmission", "GISTRAN.Trans_330kV_OH_Carrier")

0 Kudos
MittRamgobin
New Contributor

Thank you Curtis.

That worked and yes if did figure the path thing.

0 Kudos
MittRamgobin
New Contributor

I have another issue. I want to delete all empty shapefiles in a folder. The following is not working:

#Find empty shapefiles and delete them

arcpy.env.workspace = r"S:\SupportServices\LGAShapefiles"

shapefiles = arcpy.ListFeatureClasses()

for shapefile in shapefiles:

    if arcpy.management.GetCount(shapefile)[0]=="0":

        arcpy.Delete_management(shapefile)

        print shapefile + "Deleted"

0 Kudos
curtvprice
MVP Esteemed Contributor

GetCount doesn't return a number, it returns a result object. This is documented in the help for the tool.

Try:

if str(arcpy.management.GetCount(shapefile))=="0":

# or (both will work)

if arcpy.management.GetCount(shapefile).getOutput(0)=="0":

0 Kudos
MittRamgobin
New Contributor

Still not working.

ERROR 000732: Input Rows: Dataset GISTRAN.SubstationCOOROW, SHIRE OF.shp does not exist or is not supported

Failed to execute (GetCount).

0 Kudos