Select to view content in your preferred language

Python Type Error

1023
6
11-16-2017 10:55 AM
by Anonymous User
Not applicable

Hi All. 

I am  new to python and trying to build a small script that will take a double field, find its min and max, then use that min and max to calculate another double field. Eventually I want to make the workspace, fc, and fields parameters. See attached. I'm able to print the min and max, but when I try to use the update cursor, I keep returning the following error: 

Runtime error
line 19, in <module>
row[1] = (([0] - minimum) / (maximum - minimum))
TypeError: unsupported operand type(s) for -: 'list' and 'float'

I looked it up, but need some more direction. 

Any help is appreciated!

Thank you!

Script attached

0 Kudos
6 Replies
RebeccaStrauch__GISP
MVP Emeritus

Not seeing the entire script  ( see https://community.esri.com/people/curtvprice/blog/2014/09/25/posting-code-blocks-in-the-new-geonet?s...‌ )

i would try  changing  [0]   To row[0]

by Anonymous User
Not applicable

Thank you so much! That worked! Do you know the best way to set the fields to be parameters? this returns an error as well, I'm sure I'm just doing the syntax wrong. I cannot get the script to attach, does this help?

import arcpy

arcpy.env.workspace = arcpy.GetParameterAsText(0)
fc = arcpy.GetParameterAsText(1)
valuefield = arcpy.GetParameterAsText(2)
rankfield = arcpy.GetParameterAsText(3)

maximum = max(row[0] for row in arcpy.da.SearchCursor(fc, [valuefield]))
print maximum


minimum = min(row[0] for row in arcpy.da.SearchCursor(fc, [rankfield]))
print minimum

with arcpy.da.UpdateCursor(fc, [valuefield, rankfield]) as cursor:
for row in cursor:
row[1] = ((row[0] - minimum) / (maximum - minimum))
cursor.updateRow(row)

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Are you creating a tool (can be in a custom toolbox)?  You can "add a script" as a new tool, and there is a tab you setting up you input parameters.  That is how I typically do it for running in ArcMap or ArcCatalog.

edit

What is a script tool?—Help | ArcGIS for Desktop 

I like creating addin so it is easy for others to install.  Extremely helpful if complex or many installs needed. Not available in Pro though, although the tools may still run with minor tweaks.

re addins I have a blog/pts w tips somewhere can't find it right now.

0 Kudos
by Anonymous User
Not applicable

Yes, I am, I am able to get the tool to work for the workspace and fc, but unsure of how to call the field names as they are parameters

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

My appologies, our network is down so I am responding on my iPad, but some tips...

first off, make sure your get statements are in the Sam order as the are on the tool parameters page.

CaSe and indentation is VERY important with Python, do make sure the commands are in the correct case at a minimum.   And in you sample...indentation is off. After a : next line is indenting, and all the rest in the block..I pe you cursor block

my typical process (again, sorry about formatting)

myFirstParam = arcpy.GetParameterAsText(0)

if not myFirstParam:

  myFirstParam = "a default value here"  #  or an addMessag for an error

# then you can use myFirstParam in you script, if ok

i typically set my workstation to a variable then set the environment to the variable.  Most likely you will need to use the oath again somewhere in your script.  Hope thus helps.

other tips for posting questions, just FYI  https://community.esri.com/community/help-and-feedback/blog/2016/08/15/community-news-and-tips-how-t...

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Oops, sorry....just noticed you need field names.   Are you having the user select in the dialog...allowing multiple?  Then you need to split by ; I believe.   You want the field list in the dialog to be dependent on the fc selected.  Look in the tool help ...there is something in there about that.  I hAve done it...fields tend to be easy....setting dependencies on other things, not always so easy.

if you don't need the to select, you can get a list of the fields and cursor thur the...skip these that up you know you have to.

edit

Understanding script tool parameters—Help | ArcGIS Desktop 

Validating was the other topic that can control dependancies from other parameters...in the dialog itself.

0 Kudos