我目前正在尝试搜索youtube播放列表。废料工作,但我想只得到标题的一部分。
例如 :
我想删除|
之后的所有字符
可能吗?
import re
p = re.compile("(.*?) \|.*")
m = p.search('Et si on mangeait la connaissance? | Idriss Aberkane | TEDxPanthéonSorbonne')
这给出了你想要的字符串:
m[1]
如果你确定“|”你可以在每个标题中写出这样的字符
string title = "test title | about anything";
string result ="";
if(title.indexOf("|") > -1)
result = title.substring(0, test.indexOf("|"));
如果要在第一次出现“|”时删除所有内容你可以写下面的代码:
scrap_result = 'Et si on mangeait la connaissance? | Idriss Aberkane | TEDxPanthéonSorbonne' # this is the scrap result of the title you get you can user str() to be precise so you only get string is a title.
scrap_result = scrap_result[:scrap_result.find("|")] # this will give you result before the first occurrence of '|' but it includes trailing space at the end if you want to remove it use scrap_result.strip()
是的,你有两个选择:切片
String = 'Et si on mangeait la connaissance? | Idriss Aberkane | TEDxPanthéonSorbonne'
String = String[-1:-x]
更换:
String = String.replace(' | Idriss Aberkane | TEDxPanthéonSorbonne', '')