Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中从字符串中删除子字符串?_Python_String_Numbers_Web Crawler - Fatal编程技术网

在Python中从字符串中删除子字符串?

在Python中从字符串中删除子字符串?,python,string,numbers,web-crawler,Python,String,Numbers,Web Crawler,我目前面临的问题是,我有一个字符串(deeplink),我想从中提取一个子字符串: <deeplink>https://www.jsox.de/tokyo-l200/tokio-skytree-ticket-fuer-einlass-ohne-anstehen-t107728/?partner_id=M1</deeplink> <deeplink>https://www.jsox.de/tokyo-l201/ganztaegige-bustour

我目前面临的问题是,我有一个字符串(deeplink),我想从中提取一个子字符串:

   <deeplink>https://www.jsox.de/tokyo-l200/tokio-skytree-ticket-fuer-einlass-ohne-anstehen-t107728/?partner_id=M1</deeplink>

   <deeplink>https://www.jsox.de/tokyo-l201/ganztaegige-bustour-zum-fuji-ab-tokio-t65554/?partner_id=M1</deeplink>
如何仅从上面的第一个字符串中提取子字符串
t107728
? 我试过使用split和sub函数,但没有成功


你们能帮帮我吗?非常感谢您的反馈

您可以使用
re

import re
s = ['<deeplink>https://www.jsox.de/tokyo-l200/tokio-skytree-ticket-fuer-einlass-ohne-anstehen-t107728/?partner_id=M1</deeplink>', '<deeplink>https://www.jsox.de/tokyo-l201/ganztaegige-bustour-zum-fuji-ab-tokio-t65554/?partner_id=M1</deeplink>']
new_s = [re.findall('[a-zA-Z0-9]+(?=/\?)', i)[0] for i in s]

您可以使用
re

import re
s = ['<deeplink>https://www.jsox.de/tokyo-l200/tokio-skytree-ticket-fuer-einlass-ohne-anstehen-t107728/?partner_id=M1</deeplink>', '<deeplink>https://www.jsox.de/tokyo-l201/ganztaegige-bustour-zum-fuji-ab-tokio-t65554/?partner_id=M1</deeplink>']
new_s = [re.findall('[a-zA-Z0-9]+(?=/\?)', i)[0] for i in s]

您可以使用
split
功能尝试此选项:

strings = ["<deeplink>https://www.jsox.de/tokyo-l200/tokio-skytree-ticket-fuer-einlass-ohne-anstehen-t107728/?partner_id=M1</deeplink>", "<deeplink>https://www.jsox.de/tokyo-l201/ganztaegige-bustour-zum-fuji-ab-tokio-t65554/?partner_id=M1</deeplink>"]

results = [elem.split("/?")[0].split("-")[-1] for elem in strings]

print(results)

您可以使用
split
功能尝试此选项:

strings = ["<deeplink>https://www.jsox.de/tokyo-l200/tokio-skytree-ticket-fuer-einlass-ohne-anstehen-t107728/?partner_id=M1</deeplink>", "<deeplink>https://www.jsox.de/tokyo-l201/ganztaegige-bustour-zum-fuji-ab-tokio-t65554/?partner_id=M1</deeplink>"]

results = [elem.split("/?")[0].split("-")[-1] for elem in strings]

print(results)
['t107728', 't65554']