Tony Meyer said unto the world upon 2005-04-06 01:59:
s = 'Hi "Python Tutors" please help'
s.split()

['Hi', '"Python', 'Tutors"', 'please', 'help']

I wish it would leave the stuff in quotes in tact:

['Hi', '"Python Tutors"', 'please', 'help']


You can do this with a regular expression:



<SNIP re solution>

Or you can just join them back up again:


combined = []
b = []
for a in s.split():

... if '"' in a: ... if combined: ... combined.append(a) ... b.append(" ".join(combined)) ... combined = [] ... else: ... combined.append(a) ... else: ... b.append(a) ...

b

['Hi', '"Python Tutors"', 'please', 'help']

(There are probably tidier ways of doing that).

That won't work for the general case. I spent about 30 minutes trying to come up with a reliably non-re way and kept hitting bugs like the one here. Given that Tony_combine is a function wrapping Tony's logic:


>>> Tony_combine('This will not work as "more than two words" are quoted')
['This', 'will', 'not', 'work', 'as', 'than', 'two', '"more words"', 'are', 'quoted']


Happily, the other solution Tony offered works for my case and thus saves me the hassle of fixing my attempt :-)

Or you can do the split yourself:

def split_no_quotes(s):
<SNIP code which works for my test case>


Best to all,

Brian vdB

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to