This question is different to Using Calculate Field to fix hyperlinks in ArcPy gives Cannot use VB for services? which was asked previously because it is using the Python parser specifically, rather than VB.
Syntax calculates the field correctly within tool (see raw below). Copy the python snippet directly from the results window to migrate into main script, and the syntax returns as invalid. Not sure how to fix.
Raw (working):
"\\\\ftp\\raw\\" + "20160206" + "\\Win\\Charles\\" + !NAME! + ".shp"
Copied snippet verbatim (Invalid):
arcpy.CalculateField_management(outShp, "Link", """"////ftp//raw//" + "20160206" + "//Win//Charles//" + !NAME! + ".shp"""", "PYTHON_9.3","#")
Having trouble getting it to run properly, have tried adding 'r' and using different combinations of escapes and it just won't run.
1 Answer 1
Ah! Firstly consult rules for python string literals when there is a mix of both single and double quotes.
Here expression is a string that may need to be mixed of single and double quotes. You can not enclose double quotes with double quotes or single quotes with single quotes without special sanitization.
There are couple of ways you can assign your expression
in the tool when you need to mix both single and double quotes -
Enclose string that have only double quote into single quotes e.g.
'"////ftp//raw//" + "20160206" + "//Win//Charles//"+ str(!NAME!) + ".shp"'
Enclose string that have only single quote into double quotes e.g.
"'////ftp//raw//' + '20160206' + '//Win//Charles//'+ str(!NAME!) + '.shp'"
Escape (using
\
) the quotes (either single or double) e.g."\"////ftp//raw//\" + \"20160206\" + \"//Win//Charles//\"+ str(!NAME!) + \".shp\""
OR"\'////ftp//raw//\' + \'20160206\' + \'//Win//Charles//\'+ str(!NAME!) + \'.shp\'"
or'\"////ftp//raw//\" + \"20160206\" + \"//Win//Charles//\"+ str(!NAME!) + \".shp\"'
Use seperator that gives single expression e.g. parenthesis e.g.
"""("////ftp//raw//" + "20160206" + "//Win//Charles//"+ str(!NAME!) + ".shp")"""
or'''("////ftp//raw//" + "20160206" + "//Win//Charles//"+ str(!NAME!) + ".shp")'''
-
This works perfectly, just out of curiosity, if I wanted to have a dynamic parameter (such as date), how would that insert into the expression?. e.g instead of a hard coded date "20160206" Parameter would be set as date = "20160101"geodranic– geodranic2016年02月09日 07:52:07 +00:00Commented Feb 9, 2016 at 7:52
-
1@StevenFerronato Yeah! Date is
datetime
object, you can easily convert to string e.g.datetime.strftime('%m/%d/%Y')
details docs.python.org/2/library/datetime.htmlLearner– Learner2016年02月09日 07:56:20 +00:00Commented Feb 9, 2016 at 7:56
Explore related questions
See similar questions with these tags.
\\\\ftp\\raw\20160206円\\Win\\Charles\\ u"value" .shp
which was almost but not quite. (where value was the value in my!name!
field)