Skip to content Skip to sidebar Skip to footer

Python:replace Tab In Double Quotes

Hi i have line where i want to replace tab in double quotes. I have wrote script for that but it is not working as I want. My line: Q3U962 Mus musculus MRMP-mouse Optimization

Solution 1:

NOTE: This answer assumes (it is confirmed by OP) that there are no escaped quotes/sequences in the input.

You may match the quoted string with a simple "[^"]+" regex that matches a ", 1+ chars other than " and a ", and replace the tabs inside within a lambda:

import re
s = 'Q3U96  Mus musculu MRMP-mous   Optimizatio "MRMP-mouse "'
res = re.sub(r'"[^"]+"', lambda m: m.group(0).replace("\t", ""), s)
print(res)

See the Python demo

Post a Comment for "Python:replace Tab In Double Quotes"