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

2024/11/15 11:41:57

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

Answer

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/

https://en.xdnf.cn/q/119666.html

Related Q&A

How to escape escape-characters

I have a string variable which is not printing properly, I guess because it contains escape characters. How to escape these escape-characters?>>> print "[p{Aa}\\P{InBasic_Latin}\r\t\n]&q…

Python Multiprocessing a large dataframe on Linux

As shown in the title, I have a big data frame (df) that needs to be processed row-wise, as df is big (6 GB), I want to utilize the multiprocessing package of python to speed it up, below is a toy exam…

How to pass more arguments through tkinter bind

How do I pass more arguments through tkinters bind method? for the example:tk = Tk() def moveShip(event,key):if event.keysym == Down and player1.selectedCoord[1] != 9:if key == place:player1.selectedC…

Python Class method definition : unexpected indent [duplicate]

This question already has answers here:Im getting an IndentationError (or a TabError). How do I fix it?(6 answers)Closed 6 months ago.I am getting started with Django and Python so naturally Im doing …

pandas python - round() not behaving correctly

Im rounding values in a dataframe to 1 decimal place. Here is the dfVren 2015 Hsten 2014 Vren 2014 Question 1) Maten r vllagad oc…

Converting dictionary into string

d={a:Apple,b:ball,c:cat}The above dictionary I have and I want my Output like the below-mentioned resultres="a=Apple,b=ball,c=cat"Is it possible in a pythonic way then please answer it I have…

Django Getting QuerySet.values() based on condition

Lets say I have a model Class Parent and a Class Child. And child has a field called status and a ForeignKey relationship to Parent.Lets say I retrieve one parent by calling filter (so as to have a Que…

Condition checking in python with user input [duplicate]

This question already has answers here:If...else statement issue with raw_input on Python(2 answers)Closed 8 years ago.I tried taking an input from keyboard. the checking that input with an if else sta…

Triangle of T in Python

EDIT ** I cant multiply strings by an integer. Its for a homework and those were the instructions **I need to do a triangle in python using for loops or while loops(mandatory). The final output should …

Finding prime project euler

Find the 10,001st prime number.I am trying to do Project Euler problems without using copying and pasting code I dont understand. I wrote code that finds whether a number is prime or not and am trying …