2

I would like to split thanks to shlex this kind of string:

str = 'This doesn''t work' 54e+5 15 .FALSE. 'Another example of "test"'

Result expected:

  • This doesn''t work
  • 54e+5
  • 15
  • .FALSE.
  • Another example of "test"

My main issue is that the syntax uses double simple quotes '' inside a quoted string. I cannot get shlex to work. I tried the following settings:

lex = shlex.shlex(str)
lex.whitespace_split = True
lex.quotes = "'"

But it splits between the '' even if there is no whitespace character.

Thank you !!

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
thomas
  • 51
  • 1
  • 5

1 Answers1

1

Ideally, if you control how the text is generated, I would write the file as a CSV and allow the csv module to properly quote the items. Then reading it back into a list would be a cinch.

But given the text as it is, how about:

In [4]: import shlex
In [6]: text = """'This doesn''t work' 54e+5 15 .FALSE. 'Another example of "test"'"""
In [34]: [item.replace('\\"\\"',"''") for item in shlex.split(text.replace("''",'\\"\\"'))]
Out[34]: ["This doesn''t work", '54e+5', '15', '.FALSE.', 'Another example of "test"']
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 1
    Hello, this string is in a text file and I need to parse it. It is true that I forgot the triple quote in typing it here, sorry! And in my code the variable is not named str, of course. With shlex.split, it works but I loose the quote between doesn and 't'... Maybe I should try a replace("''", "\'")? – thomas May 15 '13 at 11:16