python - How to remove space after slash "/" -


i working python , want remove blank spaces in urls, in order restore broken links.

this typical case have deal with.

text https:// sr.a i/gmf

the link has 1 blank space after slash (/) can expected. can have other blank spaces randomly distributed.

first, want fix if present space after slash (/)

.replace('/ ', '//') 

this code works fine replace blank space after slash, there way fix link if blank space occurs anywhere else without removing white spaces since need preserve meaning of text?

use regexp lib https://docs.python.org/3.6/library/re.html following regexp

import re text = re.sub(r"[/]\s", "/", text) # r"" --> regexp in python # [/] --> slash # \s  --> blank 

in online regexp editor can play around make regexp more stable corner cases


Comments

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -