1

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
Jay Guarneri
2,1051 gold badge16 silver badges33 bronze badges
asked Mar 13, 2013 at 19:14
3
  • Please explain what trouble you are having. At first glance it looks like you need to handle the backslashes in your path strings. Commented Mar 13, 2013 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. Commented Mar 13, 2013 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. Commented Mar 13, 2013 at 20:18

1 Answer 1

3

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)
answered Mar 13, 2013 at 19:36
4
  • 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. Commented Mar 13, 2013 at 20:17
  • Calling crows.update(crow) should update the attribute table. Commented Mar 13, 2013 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) Commented Mar 13, 2013 at 20:28
  • 1
    Got it. Your suggestion was correct except it had to be crows.updateRow(crow) Commented Mar 13, 2013 at 21:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.