String split with " and/or ' and/or \<sep>

K

Kurt Mueller

How to (super)split a string (literal) containing " and/or ' and/or \<sep>.

example:

' a " b b " c\ c '.supersplit(' ')
->
['a', ' b b ', 'c c']


Thanks and Grüessli
 
P

Paul McGuire

How to (super)split a string (literal) containing " and/or ' and/or \<sep>.

example:

' a  "  b b   "  c\ c '.supersplit(' ')
->
['a', '  b b   ', 'c c']

Thanks and Grüessli
--
Kurt Müller:
(e-mail address removed)
re.split(r'''['"\\]''',' a " b b " c\ c ')
[' a ', ' b b ', ' c', ' c ']
 
P

Paul McGuire

How to (super)split a string (literal) containing " and/or ' and/or \<sep>.

example:

' a  "  b b   "  c\ c '.supersplit(' ')
->
['a', '  b b   ', 'c c']

Thanks and Grüessli

Or did you mean this?
re.split(r'''['"]|\\ ''',' a " b b " c\ c ')
[' a ', ' b b ', ' c', 'c ']

(In your example, you should prefix you quoted string literal with an
r, as in:

r' a " b b " c\ c '.supersplit(' ')

That way, the '\' character will be treated as just any other
character.

-- Paul
 
P

Peter Otten

Kurt said:
How to (super)split a string (literal) containing " and/or ' and/or
\<sep>.

example:

' a " b b " c\ c '.supersplit(' ')
->
['a', ' b b ', 'c c']


Thanks and Grüessli
['a', ' b b ', 'c c']

Peter
 
K

Kurt Mueller

Peter said:
Kurt said:
How to (super)split a string (literal) containing " and/or ' and/or
\<sep>.
example:

' a " b b " c\ c '.supersplit(' ')
->
['a', ' b b ', 'c c']
import shlex
shlex.split(' a " b b " c\ c ')
['a', ' b b ', 'c c']

Thanks Peter
Thanks Paul

shlex is what I was looking for.


Grüessli
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top