Expression

2009-04-07 Thread Lydia
I am working on calculating one of the fields in a feature class based on other 
2 fields. The logic is, 
A (the resulting field) is calculated from B, but C and D have part of the 
value that could fill the blank of B, which meaning that combine three fields 
of values can make A.

Field A is what I need.

The data looks like: .

A   B   C   D
 2  2
 5  5
 44
 6   6


 cur = gp.UpdateCursor(data)
row = cur.Next()
gp.CalculateField_management(data, "A", "[B]", "VB", "")
   
 while row:

cur.UpdateRow(row)

if  not(row.GetValue("C") == 'NULL'):
  row.SetValue("A",row.GetValue("C"));
  
elif not(row.GetValue("D") == 'NULL'):
  row.SetValue("A",row.GetValue("D"));

row = cur.Next()

del cur
del row

But the out looks like only B was calculated to A successfully. C&D are not in 
A. 

I guess there must be something wrong with the code, but I am very new to 
Python, and not familiar with the expression. Could anybody help ?  PS. I am 
coding Python with ARCGIS.

Thanks a lot.

Lydia
--
http://mail.python.org/mailman/listinfo/python-list


calculate field in ARCGIS

2009-04-09 Thread Lydia
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).

Thanks for any kind of help!

Lydia--
http://mail.python.org/mailman/listinfo/python-list


Re: calculate field in ARCGIS

2009-04-09 Thread Lydia

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 \"\" "


- Original Message - 
From: "Chris Rebert" 

To: "Lydia" 
Cc: 
Sent: Thursday, April 09, 2009 1:55 PM
Subject: Re: calculate field in ARCGIS


On Thu, Apr 9, 2009 at 12:42 PM, Lydia  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

--
http://mail.python.org/mailman/listinfo/python-list


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

2009-04-09 Thread Lydia
Thanks guys!  You made it work.

>On Apr 9, 12:55�pm, Chris Rebert  wrote:
> On Thu, Apr 9, 2009 at 12:42 PM, Lydia  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
--
http://mail.python.org/mailman/listinfo/python-list