1

I am trying to select records in my table that have certain words in the "Name" field and then code them as 0 (not pharmacy) or 1 (pharmacy) This is just a test searching for two words to select non-pharmacies.

I am getting a:

ExecuteError: ERROR 000622: Failed to execute (Calculate Field). Parameters are not valid. ERROR 000623: Invalid value type for parameter field.

I don't know which variable is invalid. Any thoughts? I also do not know if the Calculate Field will work on only the records selected. I cannot get far enough to check that.

# import system modules
import arcpy
import random

# set workspace environment
myWorkspace = arcpy.env.workspace = "C:/PharmacyProtocol/tables"
arcpy.env.overwriteOutput = True

# set variables
out_table = "VT_TRL_DLC.dbf"

# set field variables
fieldList = arcpy.ListFields(out_table)

for field in fieldList:
    if field.name == "Name":
        nameF = field
    elif field.name == "Pharmacy":
        pharmF = field

#create list of words that are NOT pharmacies
noPharmWords = ['AMERICAN LEGION','BAR']

expression = 0

#find words in Name field, code Pharmacy as "0" if not already coded
for codeWord in noPharmWords:

#create SQL text clause
fldName = '"Name"'
endClause = "%'"
SQLstr = fldName + " LIKE '%" + codeWord + endClause

#select records from table with codeWord
cursor = arcpy.da.SearchCursor(out_table, ["Name"], SQLstr)

arcpy.CalculateField_management(out_table, pharmF, expression)

del cursor
1
  • Looks like you have some minor formatting issues, need a indentation after > for codeWord in noPharmWords: Also you have some dead code in this loop > for field in fieldList:
    – artwork21
    Jul 30 '14 at 21:03
2

You seem to be misusing the cursor. arcpy.CalculateField_management() will operate on the entire table or table view it is given, regardless of any cursor object's existence. It's probably failing because you're passing a field object rather than a field name to it.

Try using an update cursor instead, replacing this:

#select records from table with codeWord
cursor = arcpy.da.SearchCursor(out_table, ["Name"], SQLstr)

arcpy.CalculateField_management(out_table, pharmF, expression)

del cursor

with this:

#select records from table with codeWord
with arcpy.da.UpdateCursor(out_table, ["Name","Pharmacy"], SQLstr) as cursor:
    for row in cursor:
        row[1] = expression
        cursor.updateRow(row)
1

Is the field an integer type, you might try expression = int(0). Calculate field will work on selected records only or all records if none are selected. Will Calculate field 0 work from the attribute table for that field?

Only other option I can suggest is to comment out the CalcField function and maybe do something else that proves the cursor is working as intended.

0

To determine why you are receiving that error I would recommend replacing:

arcpy.CalculateField_management(out_table, pharmF, expression)

with:

print out_table
print pharmF
print expression
arcpy.CalculateField_management(out_table, pharmF, expression)

I suspect that you are not supplying CalculateField_management with the parameter values that you think you are.

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.