1

I have a line type shapefile representing the road network of a country. That the country has experienced more change in the road names is why I want to create a script that will help me make update the attribute table of the shapefile based on an excel file as a data source

excel file has three fields [RoadName, District, City]

the attribute table has four fields [ROAD_NAME, District_Name, CITY_NAME, ID_Road]

CITY and DISTRICT are input parameters (GetParametreAsText) CITY 1 = input value DISTRICT 2 input value = Example: enter image description here enter image description here

in this case the script should be able to check if the input value CITY corresponds to the attribute table of the shapefile if you must assign the correct value (input value) whether it is necessary to check whether the District corresponds to the one in the attribute table if you must assign the right value (value input2) if the value of the district (District == Input value 2) check in later RoadName if it matches ROAD_NAME if yes go to the second line.

I've tried this code but not working

import arcpy
CityName=arcpy.GetParameterAsText(0)
DistrictName=arcpy.GetParameterAsText(1)
cursor1=arcpy.da.UpdateCursor("ExcelFile$",["CITY","DISTRICT","RoadName"])
cursor2=arcpy.da.SearchCursor("ExcelFile$","ROAD_Name")
cursor3=arcpy.UpdateCursor("Shapefileroads")
dict={}
for row in cursor1:
    if row[0] ==CityName:
        if row[1]==DistrictName:
            pass
        else:
            row[1]=DistrictName
            cursor1.updateRow(row)
    else:
        row.CITY_Name=CityName
        cursor1.updateRow(row)
for row2 in cursor2:
    dict={row2}
for row3 in cursor3:
    if not row3 in dict:
    row3.ROAD_Name=row2[0]
    cursor3.updateRow(row3)
  • 1
    When you run your code what happens? Have you been able to use a search cursor to successfully read and print values from your Excel file before trying something more complex? – PolyGeo Apr 23 '16 at 2:35
  • 1
    "not working" does not tell us anything. – user2856 Apr 23 '16 at 10:07
  • I can store the Elements of the excel file in a dictionary but when I try to compare them with those of the feature class I can not – Lahlou Imane Apr 23 '16 at 10:30
  • Please use the edit button beneath your question to revise it with any requested clarifications. – PolyGeo Apr 23 '16 at 13:55
1

One thing that I can see that is wrong with the approach you are using is with this section:

for row2 in cursor2:
    dict={row2}

Here you are just replacing the dict variable with a dictionary of a tuple (row). A dictionary requires a key that you assign a value to.

dict[1]=row

Then anytime you pass the key 1 to the dictionary it returns the row. You might be trying to create a list instead.

exampleList = []
exampleList.append(row)

Another problem is here:

for row3 in cursor3:
    if not row3 in dict:
        row3.ROAD_Name=row2[0]
        cursor3.updateRow(row3)

row2 is used in the previous loop. Wherever that loop ends is the current value of row2, so you are continually setting ROAD_name to the last value of that row2 every got.

Otherwise, I'm still not entirely clear on what you are trying to achieve.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.