calculate field in ARCGIS
MRAB
google at mrabarnett.plus.com
Thu Apr 9 17:58:05 EDT 2009
Lydia wrote:
> Thanks for the suggestion.
>
> But I guess under Python this doesn't work. I tried putting the code in
> different ways. But still not worked.
>
> codeblock = "def codefun(code): \\
> if code == 0: \\
> return \"B\" \\
> else: return \"\" "
>
>
You might also want to try a triple-quoted string, which makes it
clearer:
codeblock = """def codefun(code):
if code == 0:
return "B"
else:
return ""
"""
> ----- Original Message ----- From: "Chris Rebert" <clp2 at rebertia.com>
> To: "Lydia" <cssaua at hotmail.com>
> Cc: <python-list at python.org>
> Sent: Thursday, April 09, 2009 1:55 PM
> Subject: Re: calculate field in ARCGIS
>
>
> On Thu, Apr 9, 2009 at 12:42 PM, Lydia <cssaua 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
>
More information about the Python-list
mailing list