[英]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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.