Python node split string

Can anyone tell me why this simple script doesn’t work - I’m a beginner with python, I’ve tried looking at other custom python nodes to match the input procedure but just cant get it to do anything.

The ultimate purpose of this will be part of a script to take a file path as input and move up a level for the output, by reversing the path , deleting up to the first \ character (i couldn’t find a node that does that starting from the right) then reversing it again. If anyone knows of a simpler method that would be most helpful.

eg input = “G:\4900\4976 Weavers Quay”
output = “G:\4900”

reverseme%20python%20script

Enable Python support and load DesignScript library

import clr
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *

The inputs to this node will be stored as a list in the IN variables.

dataEnteringNode = IN

Place your code below this line

Python code to reverse a string

using loop

revme = IN[0]
def reverse(revme):
str = “”
for i in revme:
str = i + str
return str

OUT = str

Assign your output to the OUT variable.

Hi,

Not a big user of Python to manipulate strings, but :

  • Your indentation is wrong (you have an extra space in line 12)
  • Your output is a variable thatis not yet defined. You should either : changeOUT = str by OUT = reverse(revme) OR delete your line 13 and indent correctly lines 14 to 17

Hi @Phillip_Sheridan,

If the result you want is G:\4900, some OOTB nodes can help you.

An alternative approach would be to only remove the last part of your path, that could be done like in the bottom codeblock, or if only the first part is needed in the top codeblock:

Thanks but I want it to work on any inputted filepath of unknown string length

String.Split and the proposal of @Jonathan.Olesen will work in this case.

doesn’t the line str = i + str not define that variable ?

Variables created in a function are not defined outside that function.

You only defined str in the reverse function. Currently, you are just defining a function, then you are asking from Python to output a variable that has not been defined yet.

That looks simpler for a python novice to do, thanks.

Be aware that there currently is a bug in the string.remove node and you therefore need to use it in a codeblock as I have shown… More about this can be seen here:

And if you wish it to be even smaller nodetocode helps you…
image

t1 = DSCore.String.AllIndicesOf(str, "\\");
DSCore.String.Remove(str, t1[1]);
2 Likes

I gave up on python - couldn’t get it to work even with instructions. I need to read up some more, I think.

the dynamo nodes method worked a treat thanks guys

Hi @Phillip_Sheridan

How about this?

Does that work on longer varying paths, such as this:
D:\Phil Sheridan\Documents\revit Dynamo Scripts and objects\Dynamo\Dynamo - Revit Files for Running Dynamo Graphs From\XXXX Project Template Folder

?

My solution would :wink:

Yes you just need to tweak little bit:

What I want it to do is go up 1 or 2 directories from any inputted path, if I’m interpreting your code correctly thats just counting 14 characters from the beginning. The code block and nodes method works for me - I’m trying to improve my non existent python skills, so interested to see how it works in python.

I used that one - thanks

Prepare Model For Issue V2 181029c .dyn (185.3 KB)

Here’s the finished graph which updates the revision and date on splash screen, then saves and renames the model in a separate dated issued folder and deletes all views sheets and links (except splash screen) in the new file. Getting the graph to do a purge all automatically has proved problematic tho - it seems easier to do manually than the dynamo way

You could change the python code to the following then you just change the full path and the number input for which section of the path you are after.

2 Likes

Follow @Brendan_Cassidy advice. Cheers!

1 Like