Matching a string up to a word
Peter Hansen
peter at engcorp.com
Thu Sep 5 08:37:47 EDT 2002
Bengt Richter wrote:
> On Wed, 04 Sep 2002 22:31:46 -0400, Peter Hansen <peter at engcorp.com> wrote:
>
>>I was thinking of something like that. This should do it then:
>>
>> filename = ' '.join(field.split(' ')[1:-4])
>>
>>That is, provided you never expect to see more than one consecutive
>>space in your filenames...
>
> Why should that hurt, if the head and tail items are consistently separated
> by single spaces?
>
> >>> line = '12345 /some weird/path with multiple- -spaces Wed Sep 4 2002.'
> >>> line.split(' ')
> ['12345', '/some', '', 'weird/path', 'with', 'multiple-', '', '', '', '-spaces', 'Wed', 'Sep', '4', '2002.']
> >>> line.split(' ')[1:-4]
> ['/some', '', 'weird/path', 'with', 'multiple-', '', '', '', '-spaces']
> >>> ' '.join(line.split(' ')[1:-4])
> '/some weird/path with multiple- -spaces'
Oops! I was going by the behaviour with just .split(), which I tried
originally (but which does other whitespace too), and didn't realize the
explicit .split(' ') would do what it does. Cool. :)
-Peter
More information about the Python-list
mailing list