简体   繁体   English

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

[英]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 提前致谢

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

You aren't very specific about the criteria, but it seems like you want to keep the first part up to "_geometry" and discard everthing else. 你对标准不是很具体,但似乎你想把第一部分保持为“_geometry”并丢弃其他的标准。 That is easily done with index : 使用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.

相关问题 如何删除某个字符后的所有内容? - How do I remove everything after a certain character? 如何在字符串“ On x John write:”之后剥离所有内容 - How to strip everything after the pattern “On x John wrote:” in a string 如何删除字符串中某个字符后的所有内容? - How to delete everything after a certain character in a string? 如何使用bs4在字符串“ Results for 27th July 2019”中剥离“ Results for”? - How do I strip off “Results for ” in string “Results for 27th July 2019” using bs4? 如何在Python中的一定数量的单词后剥离字符串 - How to strip a string after a certain amount of words in python 如何删除一组字典中所有字典的字典中某个值后某个字符的所有内容? - How do I remove everything after a certain character in a value in a dictionary for all dictionaries in a group of dictionaries? 如何将一定长度后的字符串中的所有内容移动到列表中 python - How to move everything in a string after a certain length into a list python 如何删除字符串中最后一个数字之后的所有内容(某些字符除外) - How to remove everything (except certain characters) after the last number in a string 我如何在正则表达式及其之后的所有内容中拆分 - How do I split at a Regex and everything after it 如何剥离[]中的所有内容 - How to strip of everything in []
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM