0

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).

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jun 13, 2017 at 19:35
11
  • Welcome to GIS stack exchange. Consider taking the tour to learn more about the site. What error messages are you getting? Commented Jun 13, 2017 at 19:44
  • Syntax error - invalid syntax Commented Jun 13, 2017 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. Commented Jun 13, 2017 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. Commented Jun 13, 2017 at 20:24
  • 1
    what line is the error at? provide a detailed description of error Commented Jun 13, 2017 at 20:24

2 Answers 2

2

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.

answered Jun 13, 2017 at 20:01
1
  • I have changed my variable to a1, a11, and a2. It is still giving my the syntax error :( Commented Jun 13, 2017 at 20:18
1

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.

answered Jun 13, 2017 at 21:12
3
  • 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. Commented Jun 14, 2017 at 0:45
  • IMG_1111 appears nowhere in the code above. Commented Jun 14, 2017 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 Commented Jun 14, 2017 at 14:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.