I need to rename a list of feature classes (each one has only one feature) based on attribute data, i face an error says ExecuteError: ERROR 000840: The value is not a Data Element. for last line in this code... please advice

6154
21
03-12-2015 08:06 AM
Mohamed_AbdelhamidHassan
New Contributor

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")

0 Kudos
21 Replies
JosephKnieriem
New Contributor

Only once you exit the with block though.  You're doing your rename inside the with block.

0 Kudos
VandanaRaghunathan
New Contributor III

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

0 Kudos
Mohamed_AbdelhamidHassan
New Contributor

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)

>>>

0 Kudos
VandanaRaghunathan
New Contributor III

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

0 Kudos
Mohamed_AbdelhamidHassan
New Contributor

Hi Vandana,

This is a screen shot of attribute highlighting the needed columns. it has a str value.

Thanks,

Mohamed

0 Kudos
Mohamed_AbdelhamidHassan
New Contributor

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?

0 Kudos
DanPatterson_Retired
MVP Emeritus

could it be Unicode encoding?  the underscore may not be recognized and needs to be changed

>>> 'hello' +u'_' + 'there'

u'hello_there'

>>>

0 Kudos
Mohamed_AbdelhamidHassan
New Contributor

for disname +' - ' + subname, it is dash not underscore.

0 Kudos
DanPatterson_Retired
MVP Emeritus

try this then...stretch our imagination and try some combinations

>>> 'hello' + u'-' + 'there'

u'hello-there'

>>> u'hello' + u'-' + u'there'

u'hello-there'

>>>

0 Kudos
VandanaRaghunathan
New Contributor III

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

0 Kudos