![]() |
String Manipulation
I need a piece of code that takes a string like this string1 =
"aaa/bbb/ccc/dd" and extracts a string containting the character after the last "/" So for this example the result would be "dd" like this: for i=0; string1.right(i) != '/'; i++ result = string1.mid(i, string1.length()) but in python. |
Re: String Manipulation
| I need a piece of code that takes a string like this
| string1 = "aaa/bbb/ccc/dd" and extracts a string containting | the character after the last "/" | | So for this example the result would be "dd" | ... lamar_air ... Here is one way ... >>> str_in = 'aaa/bbb/ccc/dd' >>> >>> list_in = str_in.split( '/' ) >>> >>> last_element = list_in[ -1 ] >>> >>> print last_element dd >>> -- Cousin Stanley Human Being Phoenix, Arizona |
Re: String Manipulation
On Tue, Jul 15, 2003 at 01:58:09PM -0700, Cousin Stanley wrote:
> | I need a piece of code that takes a string like this > | string1 = "aaa/bbb/ccc/dd" and extracts a string containting > | the character after the last "/" > | > | So for this example the result would be "dd" > | ... > > lamar_air ... > > Here is one way ... > > > >>> str_in = 'aaa/bbb/ccc/dd' > >>> > >>> list_in = str_in.split( '/' ) > >>> > >>> last_element = list_in[ -1 ] > >>> > >>> print last_element > dd > >>> > > > -- > Cousin Stanley > Human Being > Phoenix, Arizona > In Unix, >>> os.path.basename("aaa/bbb/ccc") 'ccc' Inyeol |
Re: String Manipulation
John Hunter wrote:
>>>>>>"lamar" == lamar air <lamar_air@hotmail.com> writes: > > > lamar> I need a piece of code that takes a string like this > lamar> string1 = "aaa/bbb/ccc/dd" and extracts a string > lamar> containting the character after the last "/" > > One good way to do this is to split the string on the '/' character, > which creates a list of strings > > >>> string1 = "aaa/bbb/ccc/dd" > >>> parts = string1.split('/') > >>> parts > ['aaa', 'bbb', 'ccc', 'dd'] > > Now you just want to get the last element of this list; python allows > you to index with -1 to get the last element, -2 to get the second to > last, etc... > > >>> parts[-1] > 'dd' > > JDH > While is is perfectly acceptable, you might want to consider another solution if it is *path names* you are manipulating: >>> import os >>> os.path.split("aaa/bbb/ccc/dd") ('aaa/bbb/ccc', 'dd') >>> os.path.split("aaa/bbb/ccc/dd")[1] 'dd' because this will work correctly on other platforms too when the path separator is not '/'. --Irmen de JOng |
Re: String Manipulation
I know a few people replied to this already, but here's one additional
possibility. If the data in string1 is a file path for your platform, then you can use the os.path module. >>> import os.path >>> os.path.split("aaa/bbb/ccc/dd") ('aaa/bbb/cc', 'dd') >>> os.path.splitext("filename.ext") ('filename', '.ext') The other suggestions will of course work, but if you data is a file path, then using the os.path module should be a more portable solution. Brandon "lamar_air" <lamar_air@hotmail.com> wrote in message news:2c6431ab.0307151223.4173c4ee@posting.google.c om... > I need a piece of code that takes a string like this string1 = > "aaa/bbb/ccc/dd" and extracts a string containting the character after > the last "/" > > So for this example the result would be "dd" > > like this: > for i=0; string1.right(i) != '/'; i++ > > result = string1.mid(i, string1.length()) > > but in python. |
Re: String Manipulation
On 15 Jul 2003 13:23:09 -0700, lamar_air@hotmail.com (lamar_air) wrote:
>I need a piece of code that takes a string like this string1 = >"aaa/bbb/ccc/dd" and extracts a string containting the character after >the last "/" > >So for this example the result would be "dd" > >like this: >for i=0; string1.right(i) != '/'; i++ > >result = string1.mid(i, string1.length()) > >but in python. Others have posted the split() solutions. You could also search backward in Python: >>> s = "aaa/bbb/ccc/dd" >>> s[s.rfind('/')+1:] 'dd' >>> s='no_slashes' >>> s[s.rfind('/')+1:] 'no_slashes' Regards, Bengt Richter |
Re: String Manipulation
On 15 Jul 2003 19:51:56 -0700, ee01b092@ee.iitm.ernet.in (Vinoo Vasudevan) wrote:
>lamar_air@hotmail.com (lamar_air) wrote in message news:<2c6431ab.0307151223.4173c4ee@posting.google. com>... >> I need a piece of code that takes a string like this string1 = >> "aaa/bbb/ccc/dd" and extracts a string containting the character after >> the last "/" >> >> So for this example the result would be "dd" >> >> like this: >> for i=0; string1.right(i) != '/'; i++ >> >> result = string1.mid(i, string1.length()) >> >> but in python. > >How about this: >string1 = 'aaa/bbb/ccc/dd' >result = string1[string1.rfind('/')+1:] > >Hope it's helpful, Sorry Vinoo, for some reason your post did not show up for me before I posted the same solution, even though your post is dated much before mine. I guess it has to do with delays in news servers forwarding and such. Regards, Bengt Richter |
| All times are GMT. The time now is 11:54 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.