-1

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

Alexander
  • 4,527
  • 5
  • 51
  • 98

2 Answers2

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/

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
1
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'
Guinther Kovalski
  • 1,629
  • 1
  • 7
  • 15