Search code examples
pythonstrip

how do I strip off everything after a certain string


basically I want to turn something like this

FlorenceBuilding04_geometry_clean_001_Loc1_position

to

/root/world/geo/FlorenceBuilding04_model_geometry

and

FlorenceBuilding05_geometry1_clean_001_Loc1_position

to

/root/world/geo/FlorenceBuilding05_model_geometry

Code:

s = GetName()
if s.endswith( "_position" ):
    rootname = "/root/world/geo/"+s[ :- 18 ].rstrip("_")
    rootname =rootname.replace("geometry","model_geometry")

thanks in advance


Solution

  • And you could do this also:

    >>> a = 'FlorenceBuilding04_geometry_clean_001_Loc1_position'
    >>> path = '/root/world/geo/' + a.split('geometry')[0] + 'model_geometry'
    >>> print(path)
    /root/world/geo/FlorenceBuilding04_model_geometry