1

Trying to split a string by whitespace, while preserving contents enclosed in curly/smart quotes (“”), by using shlex module in Python 3.6.3. However, it does not work properly:

>>> import shlex
>>> text = 'one “two and three” four'
>>> shlex.split(text)
['one', '“two', 'and', 'three”', 'four']

Using normal quotes ("), works as expected:

>>> text = 'one "two and three" four'
>>> shlex.split(text)
['one', 'two and three', 'four']

So, how do I get shlex to work with qurly/smart quotes?

toringe
  • 1,194
  • 3
  • 12
  • 18
  • 1
    I don't think you can. You can subclass `shlex.shlex` and set the `quotes` attribute to include characters other than `'` and `"`, but it appears you can't tell it to use a pair of open/close characters. When it encounters a character in `quotes`, it expects the *same* character to end the quoted string. – chepner Dec 13 '17 at 17:43

0 Answers0