Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'm having trouble running a loop to update the values in a particular field in attribute table by reading from a csv file. Any input is greatly appreciated. The following is the sample code:

import arcpy

featureclass = "C:\\xyz"
table1 = "C:\\pqr.csv"
alpha= list(csv.reader(open(table1,'r')))

crows=arcpy.UpdateCursor(featureclass) # guess here it should be like cursor( , )

for crow in crows:
    for row in alpha:
        if crow.STNAME==row[3] and crow.ZIP==row[6]:
            if int(crow.STNUM)>=int(row[13]) and int(crow.STNUM)<=int(row[14]):
                if row[4]==crow.STTYPE and row[15]=="B":
                   # fill a field in attribute table with a particular row[] value from csv
                else:
                    if int(crow.STNUM)%2==0 and row[4]==frow.STTYPE and row[15]=="E":
                  # fill the field in attribute table with a particular row[]value from csv
share|improve this question
    
Please explain what trouble you are having. At first glance it looks like you need to handle the backslashes in your path strings. –  blah238 Mar 13 '13 at 19:44
    
You should also be using rb (read-binary) with the csv module. Consider also using a DictReader instead of the normal reader so that you can reference columns by name instead of index. –  blah238 Mar 13 '13 at 19:48
    
blah238,thanks for the response. What I'm trying here is to search a match between the csv file and a row in attribute table and when it does, it has to read a value from csv and fill it in a field of the attribute table. Does that help ? I've been successful in using the SearchCursor to find the matches but having trouble to figure out how to write the values in the attribute table. –  hydi Mar 13 '13 at 20:18

1 Answer 1

up vote 3 down vote accepted

This does not seem like a clear question to me, but I think what you are asking is that now you have the rows in the feature class and the csv data, how do you update the row? The crows variable is the cursor by the way. That is easy, you just need to add in your if-else statement:

crows.updateRow(crow)

so here is an example in your code, this is fictious of course:

if(row[4] == crow.STTYPE and row[15]=="B":
    crow.FIELD1 = "HI"
    crows.updateRow(crow)
else:
    crow.FIELD1 = "BYE"
    crows.updateRow(crow)
share|improve this answer
    
Jamie, thanks for the response. What I'm trying here is to search a match between the csv file and a row in attribute table and when it does, it has to read a value from csv and fill it in a field of the attribute table. Does that help ? I've been successful in using the SearchCursor to find the matches but having trouble to figure out how to write the values in the attribute table. –  hydi Mar 13 '13 at 20:17
    
Calling crows.update(crow) should update the attribute table. –  Jamie Mar 13 '13 at 20:18
    
I tried it but giving error. I thought something like crow.setValue(??) and crow.getValue might be used. The field that i'm trying to fill in attribute table is blank (I don't know if it matters) –  hydi Mar 13 '13 at 20:28
1  
Got it. Your suggestion was correct except it had to be crows.updateRow(crow) –  hydi Mar 13 '13 at 21:05
    
Thanks for the edit. Sorry I missed that. –  Jamie Mar 20 '13 at 17:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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