1

I'm using arcpy.CalculateField_management to populate a field. The expression:

"'https://anystate.gov/someportal/DisplayQueryResults.aspx?Prompts=Spill%20Number="!ID!"&Database=OfficeDocs&Query=DEP_Spill_Projects&QuerySet=Portal_Queries&User=Portal.DEP&Password=DEPPortal1'"

will work when entered into the Calculate Field Tool. The url is completed by pulling in values from the ID field.

However, the Python code snippet pasted into my script results in a syntax error. It's worth noting the expression is bounded by """"'....'"""" (4 double quotes outside a single quote.

I tried Midavalo's suggestion a couple of different ways. First attempt set

expression = 'https://anystate.gov/someserver/…{0}&Database=OfficeDocs&Query=DEP_Sp‌​ill_Projects&QuerySe‌​t=Portal_Queries&Use‌​r=Portal.DEP&Passwor‌​d=DEPPortal1'.format‌​(!ID!)

Resulting in invalid syntax Invalid Syntax Based on the Python link, the second try included single quotes:

'https://....'.format('!ID!') 

But the result was a syntax error Syntax Error 539

  • Remove one set of the double quotes. In python, triple-quoting around your string will then include any additional quotes within that string. """ your string here """ – Midavalo Apr 26 '17 at 21:00
  • Also """ your string that contains additional "double" and 'single' quotes should work too """ – Midavalo Apr 26 '17 at 21:04
  • Why do you think the %20 needs to be part of the format()? It's not a variable, it's part of the string itself, so should be included as part of the string. Did the rest of the code not work as per my answer? – Midavalo May 8 '17 at 0:58
  • 1
    Please always present code and errors as text rather than pictures. – PolyGeo May 19 '17 at 2:16
4

As mentioned in my comment above, adding triple quote marks around a string will allow you to use quote marks within the string

""" your string that contains additional "double" and 'single' quotes should work too """

As you are wanting to include a value from your ID field, use python string formatting and have that add the value into your string.

myString = """ using ID {0} to find a record """.format(!ID!)

The value from !ID! is inserted into your string in place of the {0}.

inTbl = "//some_server/SiteLocations.gdb/selectSiteProgramsView"
fld = "SPILLPROJ"
expression= """ 'https://anystate.gov/someportal/DisplayQueryResults.aspx?Prompts=Spill%20Number={0}&Database=OfficeDocs&Query=DEP_Spill_Projects&QuerySet=Portal_Queries&User=Portal.DEP&Password=DEPPortal1'.format(!ID!) """

arcpy.CalculateField_management(inTbl, fld, expression, expression_type="PYTHON_9.3", code_block="")
  • @Midavalo I got an indentation error. To fix it I removed the space between the double quote and single quote at the beginning ("""X') and the space before the double quotes at the end (...ID!)X""". Thanks for your help. – MarkW May 9 '17 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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