Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > String Manipulation

Reply
Thread Tools

String Manipulation

 
 
lamar_air
Guest
Posts: n/a
 
      07-15-2003
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.
 
Reply With Quote
 
 
 
 
Cousin Stanley
Guest
Posts: n/a
 
      07-15-2003
| 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


 
Reply With Quote
 
 
 
 
Inyeol Lee
Guest
Posts: n/a
 
      07-15-2003
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

 
Reply With Quote
 
Irmen de Jong
Guest
Posts: n/a
 
      07-15-2003
John Hunter wrote:

>>>>>>"lamar" == lamar air <> 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

 
Reply With Quote
 
Brandon Beck
Guest
Posts: n/a
 
      07-15-2003
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" <> wrote in message
news: 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.



 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      07-16-2003
On 15 Jul 2003 13:23:09 -0700, (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
 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      07-16-2003
On 15 Jul 2003 19:51:56 -0700, (Vinoo Vasudevan) wrote:

> (lamar_air) wrote in message news:<. 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String manipulation Chris ASP .Net 1 05-24-2004 02:19 PM
Advanced String Manipulation (C#) Aaron ASP .Net 2 01-02-2004 06:50 PM
String Manipulation Aaron ASP .Net 3 12-31-2003 03:39 PM
Perl string manipulation jared in ecs Perl 2 10-22-2003 05:36 PM
String Array Manipulation Problem Garfield ASP .Net 5 08-27-2003 10:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57