import arcpy
from arcpy import env
# Set the workspace for ListFeatureClasses
env.workspace = r"C:\Users\mohamed.abdelhamid\Desktop\Python\test\rename_test.gdb"
fcs = arcpy.ListFeatureClasses()
# Get the subdistric name from attribute
for itmes in fcs:
field1 = "DIS_ARB_NA"
field2 = "SDIS_ARB_N"
cursor = arcpy.SearchCursor(itmes)
for row in cursor:
disname = row.getValue(field1)
subname = row.getValue(field2)
arcpy.Rename_management(in_data=itmes,out_data=disname + "_" + subname,data_type="FeatureClass")
Only once you exit the with block though. You're doing your rename inside the with block.
That is a good point! I agree, it wont work inside the loop.
Mohamed Abdelhamid Hassan
Maybe try this:
import arcpy
from arcpy import env
env.workspace = r"C:\Users\mohamed.abdelhamid\Desktop\Python\test\rename_test.gdb"
fields = ["DIS_ARB_NA","SDIS_ARB_N"]
disname = []
subname = []
for fc in arcpy.ListFeatureClasses():
with arcpy.da.SearchCursor(fc,fields) as cursor:
for row in cursor:
disname.append(row[0])
subname.append(row[1])
del row, cursor
for dname in disname:
for sname in subname:
arcpy.Rename_management(fc, str(dname + "_" + sname), "FeatureClass")
Thanks,
Vandana
just a note, the feature classes within database has only one row,
while i run the last proposed code from your side Vandana, I face this msg error:
Traceback (most recent call last):
File "C:\Python27\ArcGIS10.2\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
exec codeObject in __main__.__dict__
File "C:\Users\mohamed.abdelhamid\Desktop\Python\test\renaming_2.py", line 21, in <module>
arcpy.Rename_management(fc, str(dname + "_" + sname), "FeatureClass")
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
>>>
Hello Mohamed,
Try removing the 'str' from the arcpy.Rename_management(fc, str(dname + "_" + sname), "FeatureClass").
The feature classes within database has only one row:
What is data type of value that is in this row? Can you paste the contents of DIS_ARB_NA and SDIS_ARB_N fields in here?
Thanks,
Vandana
Hi Vandana,
This is a screen shot of attribute highlighting the needed columns. it has a str value.
Thanks,
Mohamed
Hi Vandana,
this is the last code I have:
import arcpy
from arcpy import env
#import os
# Set the workspace for ListFeatureClasses
env.workspace = r"C:\Users\mohamed.abdelhamid\Desktop\Python\test\Egy_SubDistricts_rename.gdb"
fcs = arcpy.ListFeatureClasses()
#os.getcwd
# Get the subdistric name from attribute
for itmes in fcs:
field1 = "DIS_ARB_NA"
field2 = "SDIS_ARB_N"
cursor = arcpy.SearchCursor(itmes)
for row in cursor:
disname = row.getValue(field1)
subname = row.getValue(field2)
fullname = disname +' - ' + subname
arcpy.Rename_management(itmes,fullname)
However I face this ERROR 000840: The value is not a Data Element. for the last line of code, how do I put str as data element?
could it be Unicode encoding? the underscore may not be recognized and needs to be changed
>>> 'hello' +u'_' + 'there'
u'hello_there'
>>>
for disname +' - ' + subname, it is dash not underscore.
try this then...stretch our imagination and try some combinations
>>> 'hello' + u'-' + 'there'
u'hello-there'
>>> u'hello' + u'-' + u'there'
u'hello-there'
>>>
Hello Mohamed,
Looks like the attribute is of the type unicode which is not expected format for the name. Try this instead:
import arcpy
from arcpy import env
env.workspace = r"C:\Users\mohamed.abdelhamid\Desktop\Python\test\rename_test.gdb"
fields = ["DIS_ARB_NA","SDIS_ARB_N"]
disname = []
subname = []
for fc in arcpy.ListFeatureClasses():
with arcpy.da.SearchCursor(fc,fields) as cursor:
for row in cursor:
disname.append(row[0].encode('ascii','ignore'))
subname.append(row[1].encode('ascii','ignore'))
del row, cursor
for dname in disname:
for sname in subname:
arcpy.Rename_management(fc, str(dname + "_" + sname), "FeatureClass")
Thanks,
Vandana