Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

How to delete a last character from a string

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


dudeja.rajat at gmail

Aug 29, 2008, 11:28 AM

Post #1 of 11 (217 views)
Permalink
How to delete a last character from a string

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
--
http://mail.python.org/mailman/listinfo/python-list


mrmakent at cox

Aug 29, 2008, 11:41 AM

Post #2 of 11 (211 views)
Permalink
Re: How to delete a last character from a string [In reply to]

On Aug 29, 2:28 pm, dudeja.ra...@gmail.com wrote:
> 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

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.
--
http://mail.python.org/mailman/listinfo/python-list


kyosohma at gmail

Aug 29, 2008, 11:41 AM

Post #3 of 11 (211 views)
Permalink
Re: How to delete a last character from a string [In reply to]

On Aug 29, 1:28 pm, dudeja.ra...@gmail.com wrote:
> 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


Try something like this:

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

This works with Python 2.5.2 on Windows XP.

Mike
--
http://mail.python.org/mailman/listinfo/python-list


code at pizzashack

Aug 29, 2008, 11:46 AM

Post #4 of 11 (212 views)
Permalink
Re: How to delete a last character from a string [In reply to]

On Fri, Aug 29, 2008 at 07:28:40PM +0100, dudeja.rajat[at]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 at pythonware

Aug 29, 2008, 11:50 AM

Post #5 of 11 (211 views)
Permalink
Re: How to delete a last character from a string [In reply to]

dudeja.rajat[at]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>

--
http://mail.python.org/mailman/listinfo/python-list


dudeja.rajat at gmail

Aug 29, 2008, 11:59 AM

Post #6 of 11 (211 views)
Permalink
Re: How to delete a last character from a string [In reply to]

On Fri, Aug 29, 2008 at 7:41 PM, Mike Driscoll <kyosohma[at]gmail.com> wrote:
> On Aug 29, 1:28 pm, dudeja.ra...@gmail.com wrote:
>> 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
>
>
> Try something like this:
>
>>>> x = 'test\\'
>>>> if x.endswith('\\'):
> x = x[:-1]
>
> This works with Python 2.5.2 on Windows XP.
>
> Mike
> --
> 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?
--
http://mail.python.org/mailman/listinfo/python-list


dudeja.rajat at gmail

Aug 29, 2008, 12:02 PM

Post #7 of 11 (211 views)
Permalink
Re: How to delete a last character from a string [In reply to]

On Fri, Aug 29, 2008 at 7:59 PM, <dudeja.rajat[at]gmail.com> wrote:
> On Fri, Aug 29, 2008 at 7:41 PM, Mike Driscoll <kyosohma[at]gmail.com> wrote:
>> On Aug 29, 1:28 pm, dudeja.ra...@gmail.com wrote:
>>> 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
>>
>>
>> Try something like this:
>>
>>>>> x = 'test\\'
>>>>> if x.endswith('\\'):
>> x = x[:-1]
>>
>> This works with Python 2.5.2 on Windows XP.
>>
>> Mike
>> --
>> 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
--
http://mail.python.org/mailman/listinfo/python-list


fredrik at pythonware

Aug 29, 2008, 12:16 PM

Post #8 of 11 (202 views)
Permalink
Re: How to delete a last character from a string [In reply to]

dudeja.rajat[at]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>

--
http://mail.python.org/mailman/listinfo/python-list


steve at REMOVE-THIS-cybersource

Aug 29, 2008, 12:37 PM

Post #9 of 11 (202 views)
Permalink
Re: How to delete a last character from a string [In reply to]

On Fri, 29 Aug 2008 14:46:53 -0400, Derek Martin wrote:

> On Fri, Aug 29, 2008 at 07:28:40PM +0100, dudeja.rajat[at]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
--
http://mail.python.org/mailman/listinfo/python-list


dudeja.rajat at gmail

Aug 29, 2008, 1:00 PM

Post #10 of 11 (202 views)
Permalink
Re: How to delete a last character from a string [In reply to]

On Fri, Aug 29, 2008 at 8:37 PM, Steven D'Aprano
<steve[at]remove-this-cybersource.com.au> wrote:
> On Fri, 29 Aug 2008 14:46:53 -0400, Derek Martin wrote:
>
>> On Fri, Aug 29, 2008 at 07:28:40PM +0100, dudeja.rajat[at]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
> --
> 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
--
http://mail.python.org/mailman/listinfo/python-list


code at pizzashack

Aug 29, 2008, 1:46 PM

Post #11 of 11 (202 views)
Permalink
Re: How to delete a last character from a string [In reply to]

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.rajat[at]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))

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

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.