Select to view content in your preferred language

Python programming help!!

1255
8
04-18-2012 07:43 AM
MichelleCouden1
Deactivated User
I was wondering if anyone could please help me write a python script for doing field calculation. I know I can use the field calculator, but I need a python program to do the same thing. Calculate two fields into one field. For Example : Population per square mile into a answer field. Please help!!
Tags (2)
0 Kudos
8 Replies
DarrenWiens2
MVP Alum
You can either use the Calculate Field geoprocessing tool (basically, the field calculator in a script - one line of Python code), or use an UpdateCursor (more complicated - several lines in your Python code).
0 Kudos
MichelleCouden1
Deactivated User
For my calculation would it not be written as : POPSQMile = ([POPSQ / POPULATION])  Where POPSQMile is the answer field and POPSQ and POPULATION are the calculating fields.
0 Kudos
AnthonyTimpson2
Regular Contributor
I was wondering if anyone could please help me write a python script for doing field calculation. I know I can use the field calculator, but I need a python program to do the same thing. Calculate two fields into one field. For Example : Population per square mile into a answer field. Please help!!


Stop posting the same question over and over.. we get it you need help..

if you were to read your original post it was replied to with a viable "program"

no one likes overly needy people who keep demanding help when help has already been provided
0 Kudos
MichelleCouden1
Deactivated User
Please don't be mean. If you don't want to help me then let someone else. The program provided did not answer my question.  I am new to Python programming and I was seeking help for this program I have to write. I have posted the calculation to see if that is what will work. It is the phrasing and proper use of parenthese and brackets that I need help with.
0 Kudos
RyanForbes1
Occasional Contributor
you've got to isolate the field names with [brackets] and you don't need to include the POPQSMile =

I am pretty sure you just need to put
expression = "[POPULATION] / [SQMILE]"
0 Kudos
MichelleCouden1
Deactivated User
Thanks!! That what I was getting wrong. So what do parentheses and the quotation marks do. In the calculation you wrote the quotations tell the program to run that calculation as one. Is that right?
0 Kudos
MichelleCouden1
Deactivated User
Can anyone explain to me about Quotation marks, parentheses, and brackets in the python language. What do they do and what do they stand for. Thanks!!
0 Kudos
DarrenWiens2
MVP Alum
First, you need to decide if you are using the field calculator or actually writing a full Python script. The answer above (expression = "[POPULATION] / [SQMILE]") would go into a full script, if you choose to write one. Or, much easier, you could just use the expression [POPULATION] / [SQMILE] in the expression box of the field calculator.

Quotes group together pieces of text. Square brackets indicate fields. You should really start here and go from there if you are determined to learn Python. It's a fairly simple language, but it still takes time to learn.
0 Kudos