convert VB expression to python

6432
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
KenBuja
MVP Esteemed Contributor

Please note GeoNet Help​ help space is for questions about GeoNet itself, not about specific programming questions. I'm moving it to the Python​ space. Also, using tags when asking questions will make it easier to be searched on.

AdrianWelsh
MVP Honored Contributor

Could you try .lstrip()?

So !LOCN!.lstrip() + " " + !LOCD!.lstrip() + " " + !LOCS!.lstrip()

Would that work?

by Anonymous User
Not applicable

Thanks, I tried this and it works, but I need to remove the white spaces if there is no LOCN and or LOCD data.

0 Kudos
FreddieGibson
Occasional Contributor III

You'll want to look at the python help for concatenating strings. The simplest option would be as follows.

!LOCN!.lstrip() + " " + !LOCD!.lstrip() + " " + !LOCS!

A cleaner example would be to use string.format

"{0} {1} {2}".format(!LOCN!.lstrip(), !LOCD!.lstrip(), !LOCS!.lstrip())

String Concatenation and Formatting

Python String lstrip() Method

by Anonymous User
Not applicable

Thanks, I tried this and it works, but I need to remove the white spaces if there is no LOCN and or LOCD data.  How do I remove white space from after adding it between the fields if there is no associated data for both or either of the 1st 2 fields?

0 Kudos
AdrianWelsh
MVP Honored Contributor

You will have to use the code block to make a basic logic statement utilizing If..Then.

See this article on the basics for doing this:

Calculate Field examples—Help | ArcGIS for Desktop

and scroll down to "Using code blocks"

I am guessing that you will need to check each field if it is empty or now. I would start it like:

If !LOCN!.len() <> 0

(but you'll have to tinker around with the code to make it behave properly!)

0 Kudos
JamesCrandall
MVP Frequent Contributor

Like Adrian mentions, you will need to add some Pre-Logic code to the expression.  From the field calculator:

1. Right click the field to calculate and in the Field calculator dialog, choose the appropriate Parser (ie, Python)

2. Place a check in the "Show Codeblock" box

3. In the Pre-Logic Script Code: section, add a def() that should look something like (I'm not totally certain about the exact logic desired, but this will get close I think):

def processInput(input1, input2, input3):
  if input1 == "" or input2 == "":
     return input3
  else
     return input1.strip() + "" + input2.strip() + "" + input3.strip()

4. In the box below the Pre-Logic script area, enter in the def() name and add your field parameters:

processInput( !LOCN!, !LOCD!, !LOCS!)

Order them appropriately.

edit: apologies as I'm showing strictly from the Field Calculator dialog, which does not specifically address the original question of a field calculation from within a model or python script.  You will have to adapt this as needed.

RebeccaStrauch__GISP
MVP Emeritus

And to follow James' code, if you want to then call this within a python script..

def processInput(input1, input2, input3):  
  if input1 == "" or input2 == "":  
     return input3  
  else  
     return input1.strip() + "" + input2.strip() + "" + input3.strip()  """

# I'm showing passing three fields, but can be other input that needs to be passed
arcpy.CalculateField_management("inFC", "fieldToCal", "processInput( !field1!, !field2!, !field3!)", "PYTHON_9.3", myCode_block)
AdrianWelsh
MVP Honored Contributor

Thanks Rebecca!

[/bookmarking this for later...]

0 Kudos