TransWikia.com

ArcPy string concatenation with both forward and back slashes

Geographic Information Systems Asked by Arianne Ford on February 15, 2021

I am trying to concatenate 2 string variables Var1 and Var2 using python (converting some old ArcGIS VB-based scripts to ArcPy). I need to add a backslash between them.

arcpy.CalculateField_management(TargetDB, "myfield", '!Var1!' + '' + '!Var2!', "PYTHON")

I get an error message that says

"SyntaxError: unexpected character after line continuation character"

when the code block runs. I believe the cause of the error is that Var2 contains a forward slash character. It isn’t file or folder path related, it’s an ID field that acts as a primary key in a database (which is frustrating, but something I can’t do anything about).

Given that I can’t really modify the values of Var2 because of the database situation, does anyone have any recommendations of a way around this in Python 3 to work in ArcGIS Pro 2.6? The old VB code worked, so I am assuming this is a python specific issue.

3 Answers

You can use !Var2!.replace('<problem char>', '<replacement string>')in your calculation to modify the value held in Var2 as needed.

Answered by Anna Forrest on February 15, 2021

I could easily be wrong on this one and what you're trying be valid, but I've only ever seen examples for or personally used a calculate field expression that is a single string. As I understand it, the string provided here is passed in and then parsed, so it can contain strings itself as long as you use single quote for the outer and double for strings within it, or the reverse... or you could use triple quotes, look up python docs for more on that, or ask and I can probably find it and point you in the direction.

Try: arcpy.CalculateField_management(TargetDB, "myfield", "!Var1! + '' + !Var2!", "PYTHON")

There may still be something else in there causing issues, but I'd try that first.

Answered by John on February 15, 2021

Field Calculator and python interpreter are different tools. The former post-process what you introduce as expression and should not be treated "strictly" as a python interpreter. The error message says once processed, Field Calculator tool treats your code's + "" + as a code line continuation character (i.e., , for more details have a look at https://stackoverflow.com/questions/53162/how-can-i-do-a-line-break-line-continuation-in-python). In fact, '!Var1!' + '' + '!Var2!' is converted to !Var1!!Var2! (you can verify this by copying this to Field Calculator).

To correct this error you need to thread string expression more carefully in python interpreter. For example, '!Var1!' + ' + "\" + ' + '!Var2!' will work. If you evaluate this, it turns into !Var1! + "" + !Var2! which just works since it is a valid Field Calculator expression. Personally I find python string formatting utilities better suited and safer to generate expression, e.g., r"{} + '' + {}".format('!Var1!', '!Var2!').

Answered by fatih_dur on February 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP