Wednesday, September 12, 2012

String handling in Python

Sometimes we get a url and we need to retrieve the filename from the url ( like http://some_url/filename.mp3).

This can be done easily in python.

>>> string = 'http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjectiveC/ObjC.pdf'
 

>>> string.split('/')
['http:', '', 'developer.apple.com', 'library', 'mac', 'documentation', 'Cocoa', 'Conceptual', 'ObjectiveC', 'ObjC.pdf']
 

>>> print string.split('/')[-1]
ObjC.pdf



:) B-)
 

No comments:

Post a Comment