How to use name an output using a variable from a list

2732
3
04-21-2016 05:01 PM
KellyZiegenfuss2
New Contributor

Hi All,

I am trying to use a for loop in arcpy, but am unsure of how to name the output using the variables from the list. I have attached a snippet of the code. Please let me know of any ideas. The list are all variables that are set to shapefiles. Please let me know if you have any ideas. Thanks!

#for loop
x=[ECO, HPA, POT, NAV, OPA]

for i in x:
    arcpy.Clip_analysis(i, "Pipeline_Buffer", i + "Areas")

0 Kudos
3 Replies
DanPatterson_Retired
MVP Esteemed Contributor

do you variables have a path associated with them?

in otherwords is ECO actually c:/path/eco.shp

because if you want to append "area" to that name, then you are going to have to parse out the *.shp part and add it back

ie temporarily parse off the shp part and add it back on.  This is the most explanatory form

>>> ECO = "c:/mypath/eco.shp"

>>> ENEW = ECO[:-4]+"AREA"+".shp"

>>> ENEW

'c:/mypath/ecoAREA.shp'

Clip—Help | ArcGIS for Desktop

NeilAyres
MVP Frequent Contributor

Or (more slicing and dicing...)

>>> p = "c:/myPath/myShape.shp"

>>> p

'c:/myPath/myShape.shp'

>>> pNew = "{}{}{}".format(p.split(".")[0], "Area.", p.split(".")[1])

>>> pNew

'c:/myPath/myShapeArea.shp'

>>>

DanPatterson_Retired
MVP Esteemed Contributor

Yes, but fancy formatting is not intro level as is slicing.  But it is elegant ...

>>> ECO = "c:\mypath\eco.shp"

>>> "{}{}{}".format(ECO[:-4],"Area",ECO[-4:])

'c:\\mypath\\ecoArea.shp

Fancy, fancy formatting  for the explorer

and ensures the correct path format