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
-
Please explain what trouble you are having. At first glance it looks like you need to handle the backslashes in your path strings.blah238– blah2382013年03月13日 19:44:54 +00:00Commented 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.blah238– blah2382013年03月13日 19:48:22 +00:00Commented 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.hydi– hydi2013年03月13日 20:18:21 +00:00Commented Mar 13, 2013 at 20:18
1 Answer 1
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)
-
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– hydi2013年03月13日 20:17:24 +00:00Commented Mar 13, 2013 at 20:17
-
Calling crows.update(crow) should update the attribute table.Jamie– Jamie2013年03月13日 20:18:26 +00:00Commented 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)hydi– hydi2013年03月13日 20:28:12 +00:00Commented Mar 13, 2013 at 20:28
-
1Got it. Your suggestion was correct except it had to be crows.updateRow(crow)hydi– hydi2013年03月13日 21:05:51 +00:00Commented Mar 13, 2013 at 21:05