Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to remove the quotes of certain patterns in python string

I want to remove the quotes of certain patterns (numpy.*) in python string.
For example, I have a string

"['numpy.number', 'numpy.complex_', 'numpy.int8', 'randomStr']"

`I want to remove the inner quotes to

"[numpy.number, numpy._complex, numpy.int8, 'randomStr']"

How should I define the regex rules for it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Perhaps something like the following regular expression substitution:

>>> import re
>>> s = "['numpy.number', 'numpy.complex_', 'numpy.int8', 'randomStr']"
>>> re.sub(r"'(numpy\.[_a-zA-Z][_a-zA-Z0-9]*)'", r'\1', s)
"[numpy.number, numpy.complex_, numpy.int8, 'randomStr']"
>>>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading