[?? Probable Spam] Re: calculate field in ARCGIS

Lydia cssaua at hotmail.com
Thu Apr 9 21:46:29 EDT 2009


Thanks guys!  You made it work.

>On Apr 9, 12:55 pm, Chris Rebert <c... at rebertia.com> wrote:
> On Thu, Apr 9, 2009 at 12:42 PM, Lydia <css... at hotmail.com> wrote:
> > Hi Python users,
>
> > I ran into a problem with python coding in ARCGIS. Does anybody have the
> > experience in dealing with this?
>
> > I need to calculate NEWFIELD based on OLDFIELD under condition: if?OLDFIELD
> > == 0 then return string "B" otherwise return "".
>
> > codeblock = "def codefun(code): if code == 0: return \"B\" else: return \"\"
> > "
>
> > gp.CalculateField_management("INFILE", "OLDFIELD", "codefun(!NEWFIELD!",
> > "PYTHON", codeblock)
> > I got error:
>
> > RuntimeError:
> > exceptions.SyntaxError: invalid syntax (line 1)
> > Failed to execute (CalculateField).
>
> Might I recommend you try using the multiline equivalent (assuming
> ArcGIS supports C-style escape sequences):
>
> codeblock = "def codefun(code):\n\tif code == 0:\n\t\treturn
> \"B\"\n\telse:\n\t\treturn \"\" "
>
> Cheers,
> Chris
>
> --
> I have a blog:http://blog.rebertia.com

Looks like an error in your code:

gp.CalculateField_management("INFILE", "OLDFIELD", "codefun(!
NEWFIELD!", "PYTHON", codeblock)

Should be:

gp.CalculateField_management("INFILE", "OLDFIELD", "codefun(!
NEWFIELD!)", "PYTHON", codeblock)

Or you could fold the function into the expression using the ternary
and get rid of your code block param:

gp.CalculateField_management("INFILE", "OLDFIELD", "'B' if !OLDFIELD!
== 0 else ''", "PYTHON")
--
http://mail.python.org/mailman/listinfo/python-list


More information about the Python-list mailing list