简体   繁体   English

在python中第二次出现后删除'/'的所有出现

[英]Deleting all occurances of '/' after its 2nd occurance in python

I have a URL string which is https://example.com/about/hello/ 我有一个URL字符串,它是https://example.com/about/hello/

I want to split string as 'https://example.com', 'about' ,'hello' 我想将字符串拆分为'https://example.com', 'about' ,'hello'

How to do this ?? 这个怎么做 ??

Use the urlparse to correctly parse a URL: 使用urlparse正确解析URL:

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']

At the end of the day, the information you want are in the parts.scheme , parts.netloc and paths variables. 最终,您需要的信息在parts.schemeparts.netlocpaths变量中。

You may do this : 您可以这样做:

  1. First split by '/' 首先用“ /”分隔
  2. Then join by '/' only before the 3rd occurance 然后仅在第三次出现之前通过“ /”加入

Code: 码:

text="https://example.com/about/hello/"
groups = text.split('/')
print( "/".join(groups[:3]),groups[3],groups[4])

Output: 输出:

https://example.com about hello

Inspired in Hai Vu's answer . 启发了海武的 答案 This solution is for Python 3 此解决方案适用于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]))

There are lots of ways to do this. 有很多方法可以做到这一点。 You could use re.split() to split on a regular expression, for instance. 例如,您可以使用re.split()对正则表达式进行拆分。

>>> import re
>>> re.split(r'\b/\b', 'https://example.com/about/hello/')
['https://example.com', 'about', 'hello']

re is part of the standard library, documented here. re是标准库的一部分,在此处记录。 https://docs.python.org/3/library/re.html#re.split The regex itself uses \\b which means a boundy between a "word" character and a "non-word" character. https://docs.python.org/3/library/re.html#re.split regex本身使用\\b ,这表示“单词”字符和“非单词”字符之间的界限。 You can use regex101 to explore how it works. 您可以使用regex101探索其工作方式。 https://regex101.com/r/mY8fV8/1 https://regex101.com/r/mY8fV8/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找子字符串的所有出现,然后删除几个以前的字符直到python中所有出现的前一个空格 - Finding all occurances of a substring then deleting a few previous characters up to a previous space in python for all occurances Python&#39;IF&#39;之后的第二条语句没有动作 - Python No Action on 2nd Statement After 'IF' 查找一个字符串的第二次出现的索引,并且该字符串应使用python作为该行的开头 - Finding the 2nd occurance's index of a string and that string should be the start of the line using python 提取左边的所有内容 pandas dataframe 中第 2 次出现的字符 - Extract everything to the left 2nd occurance of a character in pandas dataframe Python:比较两个数组的所有元素并修改第二个数组 - Python: Comparing all elements of two arrays and modifying 2nd array Python:从所有tr子代中找到第二个td子代 - Python: find the 2nd td child from all tr children 如何在python中编辑csv并在第二行后添加一行,该行在除1之外的所有列中都具有相同的值 - how to edit a csv in python and add one row after the 2nd row that will have the same values in all columns except 1 Python-打印第二个参数 - Python - print 2nd argument 仅解析 1 个根密钥和第二个密钥中的所有其他数据后的 xmltodict - xmltodict after parse only 1 root key and all other data in 2nd key 之后提取第二个元素<br/> - Extract 2nd element after <br/>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM