3

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.

My code block

  • 1
    not 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 Apr 9 '18 at 21:51
  • 1
    Please always provide code as text rather than as a picture. – PolyGeo Apr 9 '18 at 22:06
  • 1
    I'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 Apr 9 '18 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 Apr 9 '18 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 Apr 9 '18 at 23:28
3

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.

| improve this answer | |

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.