Fer*_*and 2 python string indexing parsing
我试图在字符串中找到东西 - 所有这些都在数字之前,例如:
"Diablo Lord Of Destruction 9.2"
Run Code Online (Sandbox Code Playgroud)
这是来自文件的索引 file[2] = "Diablo Lord Of Destruction 9.2"
如何编写仅选择文本的代码,并在这些数字之前省略数字和任何空白区域(如下所示)?
"Diablo Lord Of Destruction"
Run Code Online (Sandbox Code Playgroud)
我知道你可以通过做这样的事情轻松地做到这一点:
contents = file[2]
print contents[0:-2]
Run Code Online (Sandbox Code Playgroud)
由于值会发生变化,我需要一个更强大的解决方案,可以处理不同大小的数字和不同长度的空白区域.
这将从字符串中删除任何数字和句号:
import re
>>> filtered = re.sub('[0-9.]*','',"Diablo Lord Of Destruction 9.2 111" )
>>> filtered
'Diablo Lord Of Destruction '
>>> filtered.strip() # you might want to get rid of the trailing space too!
'Diablo Lord Of Destruction'
Run Code Online (Sandbox Code Playgroud)