Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
1  
Didn't think this one would be that hard, but after an hour of trying the closest I got was \\\\ftp\\raw\\20160206\\Win\\Charles\\ u"value" .shp which was almost but not quite. (where value was the value in my !name! field) – Midavalo Feb 9 at 7:03
up vote 4 down vote accepted

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 -

  1. Enclose string that have only double quote into single quotes e.g. '"////ftp//raw//" + "20160206" + "//Win//Charles//"+ str(!NAME!) + ".shp"'

  2. Enclose string that have only single quote into double quotes e.g. "'////ftp//raw//' + '20160206' + '//Win//Charles//'+ str(!NAME!) + '.shp'"

  3. 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\"'

  4. 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")'''

share|improve this answer
    
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 Feb 9 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.html – SIslam Feb 9 at 7:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.