string splitting

Simon Brunning simon at brunningonline.net
Mon Oct 16 21:22:43 CEST 2006


On 16 Oct 2006 12:12:38 -0700, rdharles at gmail.com <rdharles at gmail.com> wrote:
> Hello,
> I have thousands of files that look something like this:
>
> wisconsin_state.txt
> french_guiana_district.txt
> central_african_republic_province.txt
>
> I need to extract the string between the *last* underscore and the
> extention.
> So based on the files above, I want returned:
> state
> district
> province
>
> My plan was to use .split or .find but I can't figure out how locate
> only the last underscore in the filename.

>>> spam = 'central_african_republic_province.txt'
>>> spam.split('.')[0].rsplit('_', 1)[-1]
'province'

-- 
Cheers,
Simon B
simon at brunningonline.net
http://www.brunningonline.net/simon/blog/



More information about the Python-list mailing list