Delete Chars in Python

后端 未结 4 661
清歌不尽
清歌不尽 2021-01-22 02:12

does anybody know how to delete all characters behind a specific character??

like this:

http://google.com/translate_t

into

<         


        
4条回答
  •  不要未来只要你来
    2021-01-22 02:34

    For urls, using urlparse:

    >>> import urlparse
    >>> parts = urlparse.urlsplit('http://google.com/path/to/resource?query=spam#anchor')
    >>> parts
    ('http', 'google.com', '/path/to/resource', 'query=spam', 'anchor')
    >>> urlparse.urlunsplit((parts[0], parts[1], '', '', ''))
    'http://google.com'
    

    For arbitrary strings, using re:

    >>> import re
    >>> re.split(r'\b/\b', 'http://google.com/path/to/resource', 1)
    ['http://google.com', 'path/to/resource']
    

提交回复
热议问题