輸入 -"space-service-1.1.309.02070980"
預期產出 -space-service
我試過了re.sub,translate等等,但我正在尋找更簡單的一個班輪解決方案。是否可以 ?
import re
text = "space-service-1.1.309.02070980"
>>> output = re.sub(r'\d ', '', text)
>>> print(output)
from string import digits
text = "space-service-1.1.309.02070980"
res = text.translate(無,數字)
Python版本 -3.6
uj5u.com熱心網友回復:
這將適用于問題中顯示的資料,但可能不是通用解決方案:
text = "space-service-1.1.309.02070980"
print(text[:text.rfind('-')])
輸出:
space-service
uj5u.com熱心網友回復:
def get_clean(text):
result = "".join( [ "".join(pair) for pair in regex.findall(r"(\p{L} )([-]\p{L} )?", text) ] )
return result
print( get_clean("space-service-1.1.309.02070980") )
print( get_clean("space-service-1.1.309.02070980/myfile-xyz") )
"space-service"
"sace-servicemyfile-xyz"
它應該適用于任何語言并可以轉換為在線用戶
uj5u.com熱心網友回復:
.isalpha如果字串是字母字串,您可以使用回傳 true 的字串函式
text = "space-service-1.1.309.02070980"
s = "".join(word for word in text if word.isalpha())
print(s)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/474771.html
標籤:Python python-3.x
