Convert VB to Python, Help?

4769
14
Jump to solution
08-03-2017 05:06 PM
TomMagdaleno
Occasional Contributor III

Hi all, I am new to Python and trying to make an older piece of code work.  It takes two tables, compares them, and if data in table A doesn't match table B it overwrites A with B. 

If they are equal it leaves them alone...

dim intval

if [STR_NUM] = [SITUS_NR] then

intval = " "

else

intval = [STR_NUM]

end if

If it finds a "nil" in a field in table B it clears the field in table A

dim intval

if [STR_NUM] = "nil" then

intval = " "

else

intval = [SITUS_NR]

end if

Finally it overwrites table A if it finds data in table B

dim intval

if [STR_NUM] <> " " and [STR_NUM] <> "nil" then

intval = [STR_NUM]

else

intval = [SITUS_NR]

end if

Can anyone help me convert these or at least one to Python?

Thank you!

0 Kudos
14 Replies
RandyBurton
MVP Alum

Following up on Dan Patterson‌'s response:

It is also possible to do this in 1 line (without the codeblock section):

Also, it looks like you are trying to compare strings and store the result in a field that holds a string (result_field in the photos).  If not, you may also need to do some type conversion:

[!STR_NUM!,""][!STR_NUM!==str(!SITUS_NR!)]
DanPatterson_Retired
MVP Emeritus

rvburton‌...  most don't like the latter since it wasn't tried earlier (line 11 in my previous post), but they are definitely more elegant, if you can remember the generic, the specific is easy

RandyBurton
MVP Alum

Agreed.  I hadn't noticed the [ False, True ][ Condition ] check before this post.  It has a nice elegance to it, and it will come in handy. Thanks.

0 Kudos
TomMagdaleno
Occasional Contributor III

I'm looking forward to trying these suggestions on Monday!

0 Kudos
TomMagdaleno
Occasional Contributor III

Thank you Dan and Randy for helping me out!  It works now!  Another problem I had was that in shapefile form it would error "The field is not nullable".  I had to put it in a geodatabase before the field calculations would work. 

0 Kudos