2

I am trying to fill the fields of one gdb table based on some statistics over the attributes of another shape file. As it was not possible in ARCgis directly, I am trying to use python coe for this case.

I have table A where:

Work_done Length 1 50 100

I would like to say that fill the legth field with sum of the Shape_Length in table B where "work_Done" field is equal another feild in table B.

the following code is what I am thinking and I want to use it in the field_calculator but I got failed. I am not really pro in python therfore I would be really happy if sombody could guide me.

  import arcpy

  from arcpy import env

  # Set the current workspace 
  env.workspace = "C:/Python27/ArcGIS10.1"


  table = "Corriodr_stat.dbf"
  ft = "Length"
  fc = "Corridor_test.shp"
  f1, f2 = "Corridor_S", "SHAPE_Leng"


  for row1 in sorted(arcpy.da.SearchCursor(fc, [f1, f2])):
     for row2 in sorted(arcpy.da.UpdateCursor(fc, [ft])):
        if fc.f1 <= 1:
           table.row2 = getvalueField(sum row1.f2)
        elif (fc.f1 > 1 and fc.f1 <= 60)
           table.row2 = getvalueField(sum row1.f2)
        elif (fc.f1 > 60 and fc.f1 <= 100):
           table.row2 = getvalueField(sum row1.f2)

migrated from stackoverflow.com Dec 26 '13 at 3:33

This question came from our site for professional and enthusiast programmers.

  • 1
    There are a lot of errors in your code: 1. The indentation is wrong. 2. What is valueTable? You assign a value to it, but the variable is never used. 3. fc is a string, and has no attribute Corridor_S. Maybe it would be easier to help if you post the exact error message you're getting. – aquavitae Dec 12 '13 at 13:11
  • Thank you for your reply. Well the indentation is correct in my original code just when I copied it here , I did not orginized it nicely. by 'valuetable' I mean the assigned valuein the row. The 'fc' is my Feature class and has different Attribute as' Corridor_S'. I got the error' Paratmeters are not valid' – user2355306 Dec 12 '13 at 13:24
  • 1
    Please correct the indentation in your question then. I have a few comments, which I'll post as an answer – aquavitae Dec 12 '13 at 13:34
  • i have changed the code partly. I would be happy to have your helpful comments :) – user2355306 Dec 12 '13 at 13:44
3

The basic problem is the assumption that referring to a feature classes or table by name gives it any intelligence, and to be able to write any substantial python code you need to understand basic python types, and I suggest you find a basic python tutorial (not an ArGIS/python tutorial!)

  1. fc is not a feature class, it is a string which happens to contain the path to a feature class. As far as python is concerned its just a bunch of characters, and would be treated exactly the same if you wrote fc = 'I am not a feature class'. The line rowsFc = arcpy.SearchCursor(fc) calls a function (SearchCursor) which takes the path to a feature class, opens the feature class and returns a search cursor to it.

  2. See 1. fc is a string, and strings do not have an attribute Corridor_S. A record returned by a cursor in the feature class may though, so this might run:

    for row in rowsFc:
        print(row.Corridor_S)
    
  3. valueTable is not a table, it is just a variable, so storing a value to it doesn't mean that anything is going to be written to a database table. It is possible that you want something along the lines of:

    for row in rowsTable:
        row.valueField = 42
    

Without the full error including traceback, it is not really possible to say what the error you're experiencing is, but your code certainly won't run unless you fix these errors.

  • Thank you very much for your time and good comments. Well, I am not pro in coding specially python and I am just trying to get an an idea based on the avavilabe codes. Foe instance in the following link they have caled Feature class as I did. what is your idea about it?resources.arcgis.com/en/help/main/10.1/index.html#//… – user2355306 Dec 12 '13 at 15:45
  • 1
    Nothing wrong with the syntax there, but I suggest you make sure you understand it before trying to use it. – aquavitae Dec 12 '13 at 16:30
  • I have edited the code based on your guidance. I would be thnakful if you kindly check it and give me your Feedback. thanks – user2355306 Dec 12 '13 at 16:52

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.