convert VB expression to python

6428
33
02-23-2016 07:32 AM
by Anonymous User
Not applicable

I am new to python.  I am using a model to automate a data update process.  Part of the model is to calculate a new field by concatenating 3 other fields and left trimming to remove white space as needed.  Below is the VB expression.  When I use python to automatically run this model it fails at the expression to calculate the field.   Below is the VB expression.  I cannot find an example of how this would look as a python expression.  Can anyone help?

FullStreet =

LTrim( [LOCN] &" " &  LTrim( [LOCD] & " " &  [LOCS]))

Any help would be greatly appreciated.

Sincerely,

Nita L Hester

0 Kudos
33 Replies
by Anonymous User
Not applicable

So true.  Thanks for your help.  I will keep working on it too.  I added and (input3.strip() to the first if.

Nita

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

At this point it is helpful to others for you to present some example data along with what you expect the results to look like.  If the example data can be extracts from your actual data set, all the better. 

by Anonymous User
Not applicable

Here is what the data looks like after running the following code:

As you can see if there is no LOCN or LOCD it should just put the value from LOCS in FullStreet.  It is leaving it blank.

Trentwood PL should be the value in FullStreet.  The other to elif statements are working though.

Thanks,

Nita

Also just noticed that if LOCN has no value but LOCD and LOCS do have values that is not working either.

Sorry for this, I am so new to python (2 weeks) that I am not sure what to do.

Nita

0 Kudos
DanPatterson_Retired
MVP Emeritus

as a suggestion, why not highlight all the records that don't work properly, so that they can all be seen.  This looks like another example of trying to develop one field calculator expression or script that applies to all conditions when several smaller ones might work better.  This does entail working incrementally but sometimes small steps make progress faster than one big leap.

0 Kudos
by Anonymous User
Not applicable

Ok sounds good.  maybe I should start over.  I have myself very confused.

First I am running a model with the Caluclate Field in it.

I have a VB expression that does work correctly.  See below.

Result

When I try to run the model from the toobox using a python script it will not run the VB expression.

So I need to convert this expression to python.

The last set of code used was:

and the result was:

The Rycroft St and S Baynes Rd are not populating.

So with this information can you help please?  Thanks for any help you can give.

0 Kudos
DanPatterson_Retired
MVP Emeritus

Randy has a point

Luke list comprehension aand Joshua's generator expression are coming close.  In combination with a check for nulls and stripping of string spaces, I think you have something way simpler.

You didn't appear to try Joshua's expression?  give it whirl and report the cases that it failed on.

I want to see an example Luke's

Combine the ideas with Randy's suggestion as well

As a desparate gasp... the final joined result can be parsed for double spaces

Examples

>>> " ".join([ str(i) for i in ["a",None,"b",1] if i ])

'a b 1'

AND now for something completely ridiculous........

>>> " ".join([ str(i).strip() for i in ["a",None,"b"," ", 1] if i ]).replace("  "," ")
'a b 1'
RandyBurton
MVP Alum

Also, does Geoprocessing >> Results >> Current Session provide any error messages after trying the code?

0 Kudos
by Anonymous User
Not applicable

No the code runs without error codes

0 Kudos
RandyBurton
MVP Alum

You may need to add a check for null values before joining.  Nulls may also affect the strip function.

I think Dan suggested using something like this in a different thread on joining nulls:

" ".join([str(i) for i in [ !input1!, !input2!, !input3!] if i])

by Anonymous User
Not applicable

Yes, I was trying that but do not know what to put where the " " are in the expression.  Can you give me an example for how the code should look.

0 Kudos