How to remove WindowsPath and parantheses from a string [duplicate]

假装没事ソ 提交于 2020-02-26 04:27:48

问题


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 be WindowsPath('
  • .*? - 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!