[Tutor] str.split and quotes
Tony Meyer
tameyer at ihug.co.nz
Wed Apr 6 09:02:04 CEST 2005
> 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']
Opps. You're right. This is probably even uglier (can't be bothered
tidying it up), but should work in the more general case and does combine
them up again:
def Tony_combine2(s):
combined = []
b = []
in_quotes = False
for a in s.split():
if '"' in a and in_quotes:
combined.append(a)
b.append(" ".join(combined))
combined = []
in_quotes = False
continue
elif '"' in a and not in_quotes:
in_quotes = True
if in_quotes:
combined.append(a)
else:
b.append(a)
return b
>>> Tony_combine('This will not work as "more than two words" are quoted')
['This', 'will', 'not', 'work', 'as', '"more than two words"', 'are',
'quoted']
Writing your own split or using re is definitely nicer, though.
=Tony.Meyer
More information about the Tutor
mailing list