Hi,
I'm a newbie to python and I need to extract numbers from a path name. A path name such as:
/grups.google.com/grup/cmp.lng.pythn/browse_thread/thread/8_ee63e_17del_0c12d
And I'm trying to get it to take out the 17. I thought I could just do:

det file = /grups.google.com/grup/cmp.lng.pythn/browse_thread/thread/8_ee63e_[B]17del[/B]_0c12d
index = det_file.find('del')
dent = det_file[index[-2]]

This doesnt' work, could you please advise?

Hi,
I'm a newbie to python and I need to extract numbers from a path name. A path name such as:
/grups.google.com/grup/cmp.lng.pythn/browse_thread/thread/8_ee63e_17del_0c12d
And I'm trying to get it to take out the 17. I thought I could just do:

det file = /grups.google.com/grup/cmp.lng.pythn/browse_thread/thread/8_ee63e_[B]17del[/B]_0c12d
index = det_file.find('del')
dent = det_file[index[-2]]

This doesnt' work, could you please advise?

If length of the last few characters(_0c12d) aren't going to change, you can try this:

det_file = "/grups.google.com/grup/cmp.lng.pythn/browse_thread/thread/8_ee63e_17del_0c12d"
le=len(det_file)
number=det_file[le-11:le-9]
print(number)

You were on the right track. Find the 'del' part, then include backwards until you encounter an underscore; this allows for if the number was single, double, triple, quadruple, etc digits long.

filename = '/grups.google.com/grup/cmp.lng.pythn/browse_thread/thread/8_ee63e_17del_0c12d'
end = filename.find('del')
# reverse-find the first underscore in the filename
# from the 0-index to the end we just calculated.
# we add one because we want our start to be one
# past the underscore.
start = filename.rfind('_', filename[:end]) + 1
number = filename[start:end]

Fantastic thanks guys, that is so working shadwickman. Thanks

Wow, I can't believe my previous code had such a bad mistake... start = filename.rfind('_', filename[:end]) + 1 SHOULD have been: start = filename[:end].rfind('_') + 1 Sorry for any confusion!

Hehe, well you put me on the right track, I changed your code to:

start = filename.rfind('_', 0, end)
number = filename[start+1:end]

Thanks :)

This question has already been answered. Start a new discussion instead.