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

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Apr 9, 2018 at 21:40
5
  • 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. Commented Apr 9, 2018 at 21:51
  • 1
    Please always provide code as text rather than as a picture. Commented Apr 9, 2018 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! Commented 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. Commented 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"] Commented Apr 9, 2018 at 23:28

1 Answer 1

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.

answered Apr 10, 2018 at 14:49

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.