我有一个https://example.com/about/hello/
我想将字符串拆分为'https://example.com', 'about' ,'hello'
怎么做?
答案 0 :(得分:4)
使用urlparse
正确解析网址:
import urlparse
url = 'https://example.com/about/hello/'
parts = urlparse.urlparse(url)
paths = [p for p in parts.path.split('/') if p]
print 'Scheme:', parts.scheme # https
print 'Host:', parts.netloc # example.com
print 'Path:', parts.path # /about/hello/
print 'Paths:', paths # ['about', 'hello']
在一天结束时,您想要的信息包含在parts.scheme
,parts.netloc
和paths
变量中。
答案 1 :(得分:1)
你可以这样做:
代码:
text="https://example.com/about/hello/"
groups = text.split('/')
print( "/".join(groups[:3]),groups[3],groups[4])
输出:
https://example.com about hello
答案 2 :(得分:1)
受到Hai Vu's answer的启发。此解决方案适用于Python 3
from urllib.parse import urlparse
url = 'https://example.com/about/hello/'
parts = [p for p in urlparse(url).path.split('/') if p]
parts.insert(0, ''.join(url.split('/')[:3]))
答案 3 :(得分:0)
有很多方法可以做到这一点。例如,您可以使用 public void SearchMovieTaskComplete(MovieDetails[] details) {
if(details!=null){
SearchResultFragment searchResultFragment= new SearchResultFragment();
Bundle data=new Bundle();
data.putSerializable("MovieDetail",details);
searchResultFragment.setArguments(data);
getFragmentManager().beginTransaction().replace(R.id.home_layout,searchResultFragment,"searchedResultCollected").commit();
}
}
拆分正则表达式。
Failed to decode downloaded font: http://PATH_TO_YOUR_FONT/NanumBarunGothic.woff
OTS parsing error: incorrect file size in WOFF header
re.split()
是标准库的一部分,在此处记录。
https://docs.python.org/3/library/re.html#re.split
正则表达式本身使用>>> import re
>>> re.split(r'\b/\b', 'https://example.com/about/hello/')
['https://example.com', 'about', 'hello']
,这意味着"单词"之间的区别。字符和"非字"字符。您可以使用regex101来探索它的工作原理。 https://regex101.com/r/mY8fV8/1