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).
2 Answers 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.
-
I have changed my variable to a1, a11, and a2. It is still giving my the syntax error :(Vincent Law– Vincent Law2017年06月13日 20:18:54 +00:00Commented Jun 13, 2017 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– Vincent Law2017年06月14日 00:45:26 +00:00Commented Jun 14, 2017 at 0:45 -
IMG_1111 appears nowhere in the code above.J Kelly– J Kelly2017年06月14日 13:18:45 +00:00Commented 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 picturesVincent Law– Vincent Law2017年06月14日 14:29:16 +00:00Commented Jun 14, 2017 at 14:29
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.