问题
I need to remove WindowsPath(
and some of the closing parentheses )
from a directory string.
x= (WindowsPath('D:/test/1_birds_bp.png'),WindowsPath('D:/test/1_eagle_mp.png'))
What I need to have is
x= ('D:/test/1_birds_bp.png', 'D:/test/1_eagle_mp.png')
I don't know how to do multiple strings at once.
by following @MonkeyZeus I tried;
y = [re.sub("(?<=WindowsPath\(').*?(?='\))",'',a) for a in x]
TypeError: expected string or bytes-like object
回答1:
You can easily target the paths with:
(?<=WindowsPath\(').*?(?='\))
(?<=WindowsPath\(')
- left side needs to literally beWindowsPath('
.*?
- lazily capture everything until we hit the positive lookahead(?='\))
- positive lookahead for literally')
https://regex101.com/r/GrIY4I/1/
回答2:
x= "(WindowsPath('D:/test/1_birds_bp.png'),WindowsPath('D:/test/1_eagle_mp.png'))"
x.replace('WindowsPath(','').replace('(','').replace(')','')
output:
'D:/test/1_birds_bp.png','D:/test/1_eagle_mp.png'
来源:https://stackoverflow.com/questions/60328125/how-to-remove-windowspath-and-parantheses-from-a-string