I had some VB code in one of my Calculate Field modules in modelbuilder. I am trying to convert it to Python
Can anybody help me with this?
IF RTRIM([PRE_DIR]) = " " then
MyLabel = LTRIM([STR_NAME]) & " " & [SUF_FIX]
else
MyLabel = RTRIM([PRE_DIR]) & " " & LTRIM(RTRIM([STR_NAME])) & " " & [SUF_FIX]
end If
Thanks
Solved! Go to Solution.
If you have it in model builder simply open it in model builder.
Then under Model and Export you can export it automatically to a python scrip.
If you have it in model builder simply open it in model builder.
Then under Model and Export you can export it automatically to a python scrip.
Thanks Robert! I knew there was an easy way to do this. HAHA!
I know I marked your response as correct and it may be correct but I'm still getting an error when trying to convert my if statement to python. I tried this but it isn't working
If (!PRE_DIR!).RTrim() = " "
print(!STR_NAM!) + " " + (!SUF_FIX!)
else
print(!PRE_DIR!).RTrim() + " " + LTrim (RTrim(!STR_NAME!)) + " " +
Return !SUF_FIX!
Any help would be appreciated!
Thanks
Whatever your trying to convert it does not look like it came from Model Builder.
I have used VB code in model builder expressions in the past and if they work in model builder, which I am assuming yours do, they should convert to python and the code from your last post is not python. Or are you just posting the original VB code.
I know I have the syntax wrong because I am very unfamiliar with python syntax. I have just been looking at python examples on the internet and doing what I thought would work since what I had in VB works. Here is a screenshot of the Calculate Field module I have in python.
What does the syntax look like when you open the python script after you export it as python
# Process: Calculate Field (2)
arcpy.CalculateField_management(clines__5_, "LABEL", "RTRIM([PRE_DIR]) & \" \" & LTRIM(RTRIM([STR_NAME])) & \" \" & [SUF_FIX]", "VB", "IF RTRIM([PRE_DIR]) = \" \" then\\n MyLabel = LTRIM([STR_NAME]) & \" \" & [SUF_FIX]\\nelse\\n
That looks like it should work when you run the python script.
Then I just need to learn how to enter it into the prelogic part of the module.
Thank you for helping me figure out this issue Robert!