I had initially neglected to delete my irow
object at the end of the script; after updating my code the insert cursor appears to be adding all rows (even the last one!) the way it should be. Here is the final code:
import arcpy, os
months = {'JAN':1,'FEB':2,'MAR':3,'APR':4,'MAY':5,'JUN':6,'JUL':7,'AUG':8,
'SEP':9,'OCT':10,'NOV':11,'DEC':12}
ignore = ['Arbitrary_count','TOTAL_MGY','JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC']
ws = r'D:\Data\Users\jbellino\Project\faswam\data\water_use\SC\FromTomAb\SC_WELLS_data_jcb.mdb'
arcpy.env.workspace = ws
arcpy.env.overwriteOutput = True
tbl = 'ORIGINAL_DHEC_WELL DATA'
itbl = 'monthly_dhec_well_data'
fields = arcpy.ListFields(tbl)
rows = arcpy.SearchCursor(os.path.join(ws,tbl))
irows = arcpy.InsertCursor(os.path.join(ws,itbl))
for row in rows:
for month in months:
#--for each row in the original table, and for each month stored in that row,
# create a new record in 'itbl'
irow = irows.newRow()
for field in fields:
if field.name == month:
#--if the field name refers to a month abbreviation it contains data
# first convert the month abbreviation to month number
irow.cn_mo = months[month]
try:
# then grab the data in the field and process it into the appropriate
# fields of the new table in the correct units
irow.cn_qnty_mo_va = row.getValue(field.name)*1000000
irow.cn_qnty_mo_va_mega = row.getValue(field.name)
except:
#--skip null values
pass
elif field.name not in ignore:
#--if the field name is not a month abbreviation, just copy the data
# to the new table
irow.setValue(field.name,row.getValue(field.name))
irows.insertRow(irow)
del irow,irows
del row,rows
The most efficient way to do it (i.e., not reading every single row in the table) is to do this...see the comments for explanation:
import arcpy
fiberCable = r'Orlando\Orlando_FIM_prep\FIBERCABLE'
field = "inventory_status_code"
# Go ahead and put the field name into the output list
output_fields = [field]
# Compose a where clause to limit the cursor to iterate over records where the field is null
where_clause = '{} IS NULL'.format(field)
with arcpy.da.SearchCursor(fiberCable, field, where_clause) as cursor:
# If any records are null, remove the field name from the output list and then break out of the loop.
for row in cursor:
output_fields.remove(field)
break
Assuming that you want to do this for multiple fields:
import arcpy
fiberCable = r'Orlando\Orlando_FIM_prep\FIBERCABLE'
fields = ["inventory_status_code", 'another_field_name']
output_fields = list()
for field in fields:
output_fields.append(field)
where_clause = '{} IS NULL'.format(field)
with arcpy.da.SearchCursor(fiberCable, field, where_clause) as cursor:
for row in cursor:
output_fields.remove(field)
break
By the way, never ever ever use a python keyword of any kind as a variable name (e.g., list)
Best Answer
A SearchCursor is read-only. You want to use an UpdateCursor. Also don't forget to call the cursor's
updateRow
method after setting a row's values.See Accessing data using cursors in the help for more information.