1

I'm trying to split text into a list in Python, but I want to exclude text & spaces between double quotes. In short, something like this:

def splitdq(text):
    # do spliting stuff here...

test = 'The "quick brown fox" jumps over the "lazy dog."'
print(splitdq(test))
>>> ["The", "quick brown fox", "jumps", "over", "the", "lazy dog."]

I found some solutions to that, but they either kept the quotes or just didn't work. So, is there a possible way to do just that in Python?

bemxio
  • 35
  • 6
  • 2
    Does this answer your question? [Split a string by spaces -- preserving quoted substrings -- in Python](https://stackoverflow.com/questions/79968/split-a-string-by-spaces-preserving-quoted-substrings-in-python) – Nikolaos Chatzis Dec 15 '20 at 19:21
  • yes, that's exactly what i wanted, thanks :) – bemxio Dec 15 '20 at 19:42

1 Answers1

1

You can use shlex class which makes it easy to write lexical analyzers for simple syntaxes such as

import shlex
test = 'The "quick brown fox" jumps over the "lazy dog."'
s = shlex.split(test)
for i in s:
    print(i)
Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55