Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to delete a last character from a string

2,811 views
Skip to first unread message

dudeja...@gmail.com

unread,
Aug 29, 2008, 2:28:40 PM8/29/08
to pytho...@python.org
Sorry : Earlier mail had a typo in Subject line which might look
in-appropriate to my friends


Hi,

I've a list some of whose elements with character \.
I want to delete this last character from the elements that have this
character set at their end,

I have written a small program, unfortunately this does not work:

dirListFinal = []
for item in dirList:
print item
if item.endswith('\\') == True:
item = item[0:-1] # This one I googled and
found to remove the last character /
dirListFinal.append(item)
else:
dirListFinal.append(item)


item.endswith() does not seem to be working.

Please help
--
Regrads,
Rajat

Mike Kent

unread,
Aug 29, 2008, 2:41:29 PM8/29/08
to

Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = "hello\\"
>>> s.endswith("\\")
True
>>> s[:-1]
'hello'

I hate to say "works for me", but it works for me. Perhaps you should
put some debugging statements in your code to see if that data you are
working on is what you are expecting.

Mike Driscoll

unread,
Aug 29, 2008, 2:41:45 PM8/29/08
to


Try something like this:

>>> x = 'test\\'
>>> if x.endswith('\\'):
x = x[:-1]

This works with Python 2.5.2 on Windows XP.

Mike

Derek Martin

unread,
Aug 29, 2008, 2:46:53 PM8/29/08
to dudeja...@gmail.com, pytho...@python.org
On Fri, Aug 29, 2008 at 07:28:40PM +0100, dudeja...@gmail.com wrote:
> dirListFinal = []
> for item in dirList:
> print item
> if item.endswith('\\') == True:

if item[-1] == '\\':

--
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D

Fredrik Lundh

unread,
Aug 29, 2008, 2:50:28 PM8/29/08
to pytho...@python.org
dudeja...@gmail.com wrote:

> I've a list some of whose elements with character \.
> I want to delete this last character from the elements that have this
> character set at their end,
>
> I have written a small program, unfortunately this does not work:
>

> dirListFinal = []
> for item in dirList:
> print item
> if item.endswith('\\') == True:

explicitly comparing against true is bad style; better write that as

if item.endswith('\\'):

> item = item[0:-1] # This one I googled and
> found to remove the last character /
> dirListFinal.append(item)
> else:
> dirListFinal.append(item)
>
>
> item.endswith() does not seem to be working.

item.endswith("\\") works just fine:

>>> item = "name\\"
>>> print item
name\
>>> print repr(item)
'name\\'
>>> item.endswith("\\")
True


>>> if item.endswith("\\") == True:

... print item[:-1]
...
name

instead of assuming that some builtin function is broken, try printing
the the value of item to check that it really contains what you think it
does. (use print repr(item) to see control characters etc).

</F>

dudeja...@gmail.com

unread,
Aug 29, 2008, 2:59:34 PM8/29/08
to pytho...@python.org
> --
> http://mail.python.org/mailman/listinfo/python-list
>


There is just a single \ at the end of every item. My list is as below:
['Results v1.0/', 'Results v1.1/']

so,

if x.endswith('\\'):

is that correct?

dudeja...@gmail.com

unread,
Aug 29, 2008, 3:02:36 PM8/29/08
to dudeja...@gmail.com, pytho...@python.org
On Fri, Aug 29, 2008 at 7:59 PM, <dudeja...@gmail.com> wrote:
> On Fri, Aug 29, 2008 at 7:41 PM, Mike Driscoll <kyos...@gmail.com> wrote:
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
> There is just a single \ at the end of every item. My list is as below:
> ['Results v1.0/', 'Results v1.1/']
>
> so,
>
> if x.endswith('\\'):
>
> is that correct?
>

Sorry Guys. I did rubbish here and bothered you guys.
I did not recognize that I need to check for / character and not \

Really very sorry. Working for the last 14 hrs, could not see this
properly. Its time I must go home and take rest.


Regards,
Rajat

Fredrik Lundh

unread,
Aug 29, 2008, 3:16:07 PM8/29/08
to pytho...@python.org
dudeja...@gmail.com wrote:

> There is just a single \ at the end of every item. My list is as below:
> ['Results v1.0/', 'Results v1.1/']
>
> so,
>
> if x.endswith('\\'):
>
> is that correct?

if your list contains forward slashes, maybe you should test for forward
slashes and not backward slashes.

</F>

Steven D'Aprano

unread,
Aug 29, 2008, 3:37:50 PM8/29/08
to
On Fri, 29 Aug 2008 14:46:53 -0400, Derek Martin wrote:

> On Fri, Aug 29, 2008 at 07:28:40PM +0100, dudeja...@gmail.com wrote:
>> dirListFinal = []
>> for item in dirList:
>> print item
>> if item.endswith('\\') == True:
>
> if item[-1] == '\\':

Which will fail badly if item is the empty string.


Rajat, rather than trying to invent your own code to join directory
paths, you should use the os.path module:

>>> import os
>>> os.path.join('/dir', 'x', 'y', 'z', 'file.txt')
'/dir/x/y/z/file.txt'
>>> os.path.split('/a/b/c/d/file.txt')
('/a/b/c/d', 'file.txt')


--
Steven

dudeja...@gmail.com

unread,
Aug 29, 2008, 4:00:54 PM8/29/08
to Steven D'Aprano, pytho...@python.org
> --
> http://mail.python.org/mailman/listinfo/python-list
>
Steven, Thanks so much.
I just found out that I unknowingly searching for something else i.e.
/ rather than \
Just corrected this mistake and fixed it.


--
Regrads,
Rajat

Derek Martin

unread,
Aug 29, 2008, 4:46:00 PM8/29/08
to Steven D'Aprano, pytho...@python.org
On Fri, Aug 29, 2008 at 07:37:50PM +0000, Steven D'Aprano wrote:
> On Fri, 29 Aug 2008 14:46:53 -0400, Derek Martin wrote:
>
> > On Fri, Aug 29, 2008 at 07:28:40PM +0100, dudeja...@gmail.com wrote:
> >> dirListFinal = []
> >> for item in dirList:
> >> print item
> >> if item.endswith('\\') == True:
> >
> > if item[-1] == '\\':
>
> Which will fail badly if item is the empty string.

Sure. I made 2 assumptions:

1. The OP didn't say how endswith() was failing. My assumption was it
was missing from his version of python (i.e. he's using a version that
predates the feature). I have no idea if that's possible or not...
I've used python 2.4 almost exclusively. I've run into a number of
cases where some clever trick I found on-line didn't work because it
was new in Python 2.5 (or whatever), so it was easy for me to lazily
assume that it could be the case here.

2. If his particular use case didn't exclude the possibility of an
empty string, he'd be smart enough to check that first, or wrap it in
a try block.

> Rajat, rather than trying to invent your own code to join directory
> paths, you should use the os.path module:
>
> >>> import os
> >>> os.path.join('/dir', 'x', 'y', 'z', 'file.txt')

I agree completely, though I do find that there's one thing missing
from this module's support, which your example illustrates nicely: a
way to find out what the root of the current filesystem is (for some
definition of current which I will let the reader figure out). You
explicitly wrote '/dir', which would likely be wrong on non-Unix OSes,
though IIRC it might work anyway, depending on your particular
environment (e.g. cygwin).

I usually end up doing this via the following function:

def _get_path_root(path):
'''recursive algorithm to determine the name of the root of the file
system in (hopefully) an os-independent way'''

if os.path.dirname(path) == path:
return path
return _get_path_root(os.path.dirname(path))

You can, depending on your needs, either pass it the absolute path of
your script, or the absolute path of your data file(s). For the sake
of performance, I get the real absoulte path outside function:

x = _get_path_root(os.path.realpath(foo))

0 new messages