Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shlex: Escaping quotes in Python 3

I want to split the ascii text

1 'K\^o, Suk\'e'

which is printed as the Python string

line = "1 'K\\^o, Suk\\'e'\n"

into

['1', 'K\\^o, Suk\\'e']

shlex.split(line) doesn't work, giving a ValueError("No closing quotation").

I tried adding to wordchars as follows:

>>> lexer = shlex.shlex(line)
>>> lexer.wordchars += "\\'"
>>> list(lexer)
['1', "'K\\", '^', 'o', ',', "Suk\\'e'"]

but again this is not my intention.

Without the quote, everything works as expected:

>>> line2= "1 'K\\^o, Suk\\e'\n"
>>> shlex.split(line2)
['1', 'K\\^o, Suk\\e']

How can this be fixed?

Edit: The closest I've come is:

>>> lexer = shlex.shlex(line)
>>> lexer.wordchars += "\\'"
>>> lexer.wordchars += " "
>>> lexer.wordchars += "\\^"
>>> lexer.wordchars += ","
>>> list(lexer)
['1', "'K\\^o,", "Suk\\'e'"]

which is still not right.

Edit: The example string is somewhat misleading. It should also be able to split, say

>>> l1 = "C12 H2 'Hello friend'"

shlex gets l1 no problem.

like image 672
user14717 Avatar asked Oct 23 '25 18:10

user14717


1 Answers

You can use posix mode and escapedquotes to '" instead of just "

>>> line = "1 'K\\^o, Suk\\'e'\n"
>>> lexer = shlex.shlex(line, posix=True)
>>> lexer.escapedquotes = "'\""
>>> list(lexer)
['1', "K\\^o, Suk'e"]
like image 184
Håken Lid Avatar answered Oct 26 '25 06:10

Håken Lid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!