I am fairly new to python. I'm trying to set this function into a variable, but i can't seem to get it to work.

Can you take a look at my script?

import arcpy
arcpy.env.workspace = "M:\Parks\HQ\OPERATIONS\CPI\Asset_Management\GNSS Projects\Parks Inventory\MergedParks\Ram_Falls_PP\CW_Ram_Falls_PP\CW_Ram_Fall_PP.gdb"

a2 = """def rep_field(in_fld, rep_value):
            targets = ['W:\OPERATIONS\CPI\Asset_Management\GNSS Projects\Parks Inventory\Ram Falls PP\RAM FALLS PP JULY 28 2015 JR~files','W:\OPERATIONS\CPI\Asset_Management\GNSS Projects\Parks Inventory\Ram Falls PP\RAMFALLSPP_JULY28_2015_JL~files']
            for targ in targets:
             in_fld = in_fld.replace(targ, rep_value)
            return in_fld"""

a1 = """rep_field (!Picture!,'\\\env.gov.ab.ca\Parks\CityWorks\Referenced\Ram_Falls_PP')"""

a11 = """rep_field (!Picture1!,'\\\env.gov.ab.ca\Parks\CityWorks\Referenced\Ram_Falls_PP')"""

arcpy.CalculateField_management(in_table="Bench_and_Table", field="Picture", expression = a1, expression_type="PYTHON_9.3", code_block = a2)
arcpy.CalculateField_management(in_table="Bridge", field="Picture1", expression = a11, expression_type="PYTHON_9.3", code_block = a2)

It is now giving me a

ERROR 000989: Python syntax error: Parsing error IndentationError: unindent does not match any outer indentation level (line 3) Failed to execute (CalculateField).

  • Welcome to GIS stack exchange. Consider taking the tour to learn more about the site. What error messages are you getting? – Mattropolis Jun 13 '17 at 19:44
  • Syntax error - invalid syntax – Vincent Law Jun 13 '17 at 19:50
  • 1
    Please Edit your "code" to use the code formatting button ({}), and to include the exact error, in context (you can use "Quote" for the error). I personally detest integrated development environments, but using one would likely show you your error. – Vince Jun 13 '17 at 20:20
  • 1
    Now remove all the ">" formatting debris. Better yet, post the actual code from your test environment. If we can't cut and paste the code, your job as OP isn't yet finished. – Vince Jun 13 '17 at 20:24
  • 1
    what line is the error at? provide a detailed description of error – NULL.Dude Jun 13 '17 at 20:24

It looks like your variable names are numbers. In Python, variable names need to begin with alpha or underscore. try that first.

Update: As for your second problem regarding string "\\env.gov.ab.ca\Parks"..., try using the "r" prefix to use raw string literals. It looks like your output is removing some characters. Again, if you don't want this, prefix your string with "r" like so:

refpic = r'\\env.gov.ab.ca\Parks'

For additional help, please refer to online python help docs at https://docs.python.org/, referring to the version of python you are using. There is plenty to discover on the usage of strings. Hope this helps.

  • I have changed my variable to a1, a11, and a2. It is still giving my the syntax error :( – Vincent Law Jun 13 '17 at 20:18

While I'm not 100% sure whether it's possible to stick an entire function into a variable like that, in practice, I don't think you should. rep_field is the name of your function, and that's how you'll call it in the future.

Your error means that the lines below your def rep_field(in_fld, rep_value): are not indented exactly four spaces, no more no less.

  • I finally got the script to run, but it truncates the link in the field. I want the domain to be \\\env.gov.ab.ca\Parks\CityWorks\Referenced\Ram_Falls_PP\IMG_1111, but i get \env.gov.ab.ca\Parks\CityWorks\Referenced\Ram_Falls_PP instead. my link is broken. – Vincent Law Jun 14 '17 at 0:45
  • IMG_1111 appears nowhere in the code above. – J Kelly Jun 14 '17 at 13:18
  • The def rep_field function replaces the directory to the pictures in the current domain to a new directory containing the pictures – Vincent Law Jun 14 '17 at 14:29

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.