[Tutor] shlex.split if there is an apostrophe in the string?
Steven D'Aprano
steve at pearwood.info
Sat Jan 8 01:31:34 CET 2011
Emile van Sebille wrote:
> On 1/7/2011 11:22 AM Sean Carolan said...
>> I'm practicing manipulating data with a text file. I'm trying to use
>> shlex.split to break up each line,
>
> Is there a reason not to use split directly?
>
> for line in fin:
> words = line.split()
shlex.split was specifically written to overcome a limitation of
str.split, namely that str.split doesn't understand quoting:
>>> text = "This includes some 'quoted text', as they call it."
>>> text.split()
['This', 'includes', 'some', "'quoted", "text',", 'as', 'they', 'call',
'it.']
>>> shlex.split(text)
['This', 'includes', 'some', 'quoted text,', 'as', 'they', 'call', 'it.']
Although the functions have the same name, they do different things.
--
Steven
More information about the Tutor
mailing list