I want to create 12 feature classes from a tuple, and I would like to have specific names that start form well_0
and end with well_11
.
in_FC = "E:/gis payannameh/TABU search/data/90spring_new.shp"
out_FC = r'E:\gis payannameh\Pychram_tabu\output.gdb'
well = ('10,24,38,41', '17,26,34,47', '5,7,18,25', '7,12,26,46', '2,12,23,36', '7,18,21,43', '2,13,16,39', '5,8,32,43', '1,9,17,44', '8,13,30,46', '2,14,34,37','3,15,12,9')
for i in range(len(well)):
templayer = "templayer_{}".format(i)
sql = """ {0} IN ({1})""".format("FID",well[i])
arcpy.MakeFeatureLayer_management(in_FC,"templayer",sql)
arcpy.FeatureClassToFeatureClass_conversion(templayer,out_FC,"well_{}".format(i))
but when I use ListFeatureClasses
for i=2
it shows well_10
, [well_0,well_1,well_10,well_11,well_2,...well_9]
.I would Like to create each feature class'name with its index name(i
) regularly something like [well_0,well_1,well_2,...,well_11]
.
-
1Use 01, 02 etc instead.FelixIP– FelixIP2021年09月30日 17:47:16 +00:00Commented Sep 30, 2021 at 17:47
2 Answers 2
You can use enumerate and Select_analysis
:
import arcpy, os
in_FC = "E:/gis payannameh/TABU search/data/90spring_new.shp"
out_FC = r'E:\gis payannameh\Pychram_tabu\output.gdb'
well = ('10,24,38,41', '17,26,34,47', '5,7,18,25', '7,12,26,46', '2,12,23,36', '7,18,21,43', '2,13,16,39', '5,8,32,43', '1,9,17,44', '8,13,30,46', '2,14,34,37','3,15,12,9')
for e, w in enumerate(well):
sql = """{0} IN{1}""".format(arcpy.AddFieldDelimiters(datasource=in_FC, field=arcpy.Describe(in_FC).OIDFieldName), tuple([int(v) for v in w.split(','))]) #Split the string into a list, convert to tuple
newname = 'well_{0}'.format(e)
arcpy.Select_analysis(in_features=in_FC, out_feature_class=os.path.join(out_FC, newname), where_clause=sql)
-
@BERA.Its Create
[u'well_0.shp', u'well_1.shp', u'well_10.shp', u'well_11.shp', u'well_2.shp', u'well_3.shp', u'well_4.shp', u'well_5.shp', u'well_6.shp', u'well_7.shp', u'well_8.shp', u'well_9.shp']
.and didn't changeMohammad hassan moayyedian– Mohammad hassan moayyedian2021年09月30日 10:59:06 +00:00Commented Sep 30, 2021 at 10:59 -
I dont understand what you are sayingBera– Bera2021年09月30日 11:28:05 +00:00Commented Sep 30, 2021 at 11:28
-
@BERA.I would like to when I use
arcpy.ListFeatureClasses()
inout_FC
fore=2
show mewell_2.shp
notwell_10.shp
I hope you understand.Mohammad hassan moayyedian– Mohammad hassan moayyedian2021年09月30日 14:50:03 +00:00Commented Sep 30, 2021 at 14:50 -
Not really. You have duplicate FIDs in your well list, you know that right?Bera– Bera2021年09月30日 14:55:05 +00:00Commented Sep 30, 2021 at 14:55
-
I checked I don't have duplicate FIDs in
well
.Mohammad hassan moayyedian– Mohammad hassan moayyedian2021年09月30日 15:08:51 +00:00Commented Sep 30, 2021 at 15:08
I hope I understand your confusion. Unexpected result caused by the way text variables are sorted. This code:
List1,List2 =[],[]
for i in range (11):
List1.append('W_%i' %i)
List2.append('W_%s' %str(i).zfill(2))
List1.sort()
List2.sort()
for item in zip(List1,List2):
print '%s%s%s' %(item[0].rjust(5),chr(9),item[1])
outputs:
W_0 W_00
W_1 W_01
W_10 W_02
W_2 W_03
W_3 W_04
W_4 W_05
W_5 W_06
W_6 W_07
W_7 W_08
W_8 W_09
W_9 W_10
You need to add leading zero in string format, so, simply change last bit of your code from:
(templayer,out_FC,"well_{}".format(i)
to:
(templayer,out_FC,"well_%s" %str(i).zfill(2)