string splitting

George Sakkis george.sakkis at gmail.com
Wed Oct 18 08:05:04 CEST 2006


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

def extract(s):
    return s[s.rfind('_')+1:s.rfind('.')]


George




More information about the Python-list mailing list