[英]How can I remove the string from a character in python?
我有一些 URL,我需要將其中一些從問號 (?)
前任。 https://www.yelp.com/biz/starbucks-san-leandro-4?large_photo=1
我需要它返回https://www.yelp.com/biz/starbucks-san-leandro-4
我怎樣才能做到這一點?
您也可以使用.split()
方法
split()方法將字符串拆分為列表。
您可以指定分隔符,默認分隔符是任何空格。
句法
string.split(separator, maxsplit)
data = 'https://www.yelp.com/biz/starbucks-san-leandro-4?large_photo=1'
print (data.split('?')[0])
輸出:
https://www.yelp.com/biz/starbucks-san-leandro-4
您可以使用rfind
並將字符串切成返回的索引:
s = 'https://www.yelp.com/biz/starbucks-san-leandro-4?large_photo=1'
s[:s.rfind('?')]
# 'https://www.yelp.com/biz/starbucks-san-leandro-4'
我將解析該URL,並使用您想要保留的部分進行重建。 例如,您可以使用urllib.parse
string =“”表示我在url中:如果i!=“?”:字符串+ = i
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.