我如何剥离某个字符串后的所有内容

how do I strip off everything after a certain string

基本上我想转成这样

FlorenceBuilding04_geometry_clean_001_Loc1_position

/root/world/geo/FlorenceBuilding04_model_geometry

FlorenceBuilding05_geometry1_clean_001_Loc1_position

/root/world/geo/FlorenceBuilding05_model_geometry

代码:

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

提前致谢

你也可以这样做:

>>> 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

您对标准不是很具体,但似乎您想将第一部分保留为“_geometry”并丢弃其他所有内容。使用 index:

很容易做到这一点
>>> def convert(example):
...     print "/root/world/geo/%s_model_geometry" % example[:example.find("_geometry")]
... 
>>> convert("FlorenceBuilding04_geometry_clean_001_Loc1_position")
/root/world/geo/FlorenceBuilding04_model_geometry
>>> convert("FlorenceBuilding05_geometry1_clean_001_Loc1_position")
/root/world/geo/FlorenceBuilding05_model_geometry