I have a parameter in a ModelBuilder model I am running that defines the geodatabase that all of the temporary files will be written to. I need to be able to call that variable in a field calculator that I run at one point. I used arcpy.da.TableToNumpyArray and need that geodatabase to be called in as the path to find the file / field. I have what I believe should be functioning code below but I keep getting a syntax error at line 3.
-
1not sure, glancing at this from the field on my phone, but if GDB1 is sending with a \\ and then you + "\\..." you end up with \\\\ in the path. that maybe a problem.ed.hank– ed.hank2018年04月09日 21:51:36 +00:00Commented Apr 9, 2018 at 21:51
-
1Please always provide code as text rather than as a picture.PolyGeo– PolyGeo ♦2018年04月09日 22:06:15 +00:00Commented Apr 9, 2018 at 22:06
-
1I'll have to guess here, but I'm pretty sure you can't call an inline variable like that in Python code. You're telling the function now to look for a file that literally ends with "%Value%", which it doesn't as that inline variable is replacing that part of the name. As for a possible solution; my guess is you will have to pass the value through to the function as an argument and than use concatenation (and possibly conversion) to add the variable to the file path. Much like you've already done for the GDB pathname!RJJoling– RJJoling2018年04月09日 22:51:41 +00:00Commented Apr 9, 2018 at 22:51
-
Your variable %value% is out of scope in the code block, include it as one of the parameters that is passed to the function.. @RJJoling is correct. Perhaps you'd like to expand your comment to an answer RJJoling. Ed.hank is also right, GDB.replace('\','\\') will cause an error: a string cannot end in ,円 see stackoverflow.com/questions/11168076/… get rid of that line, at best it does nothing but as written it will throw an error.Michael Stimson– Michael Stimson2018年04月09日 22:58:12 +00:00Commented Apr 9, 2018 at 22:58
-
This is a strange application indeed. Besides the previous suggestions, for arcpy.da.TableTonumpy... the field shape_length should be a list of strings. Try ["shape_length"]Ben S Nadler– Ben S Nadler2018年04月09日 23:28:09 +00:00Commented Apr 9, 2018 at 23:28
1 Answer 1
So I got it to work this morning. None of the Model Builder variables need to be defined in Python; the error was a result of the file path using single black slashes while Python needs double or forward slashes for file paths.
Expression
sum(Pipe_array[!SHAPE_Length!])
Code Block
Pipe_array = arcpy.da.TableToNumPyArray(r"%GDB Path%" + r"\Service_Pipes_%Value%", "SHAPE_Length")
Converting the variable to a raw string seemed to resolve the black slash issue.
Explore related questions
See similar questions with these tags.