Hello.
I'm new at Python and want to delete multiple lines in Plaxis 2D with Python. The lines between Line_11 and Line_20 that I want to delete.
I tried a few codes like below, but I couldn't successfully.
For kk in range (11,21): lines= "g_i. Line_"+str(kk) g_i.delete(lines)
Thanks
Sam.
Try using eval. So for example
for kk in range (11,21):
lines="g_i.Line_" + str(kk)
eval("g_i.delete(%s)"%(lines))
Answer Verified By: John
Thank you. I appreciate it
Dear Sam,
I have a few comments before I suggest one of the ways to delete lines in PLAXIS with Python.
In your code you use the range function to iterate over the number between 11 and 20 and then add this suffix to the object name in PLAXIS.
Since your lines seem to be created consecutively, you can use the Lines object that contains all the line objects as a list. This means that you can make use of the nice slicing that Python offers for lists, e.g.:
for line in g_i.Lines[11: 21]: # rest of code
About the way to call this object you can simply now do the following:
for line in g_i.Lines[11: 21]: g_i.delete(line)
Note that Line_11 is an object and not a string, therefore the approach you follow will probably give an error when trying to delete it, i.e. delete a string not a PLAXIS line object.