How to delete a ast character from a string?
dudeja.rajat at gmail.com
dudeja.rajat at gmail.com
Fri Aug 29 14:49:01 EDT 2008
On Fri, Aug 29, 2008 at 7:40 PM, Chris Rebert <cvrebert+clp at gmail.com> wrote:
> On Fri, Aug 29, 2008 at 11:25 AM, <dudeja.rajat at gmail.com> wrote:
>> 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:
>
> You san simplify that line to just:
> if item.endswith('\\'):
>
>> item = item[0:-1] # This one I googled and
>> found to remove the last character /
>
> And you don't need the leading 0, so just use:
> item = item[:-1]
>
>> dirListFinal.append(item)
>> else:
>> dirListFinal.append(item)
>
> And those last 3 lines are a bit redundant. Just put one
>
> dirListFinal.append(item)
>
> at the same indentation level as the "if" and delete those 3 lines.
>
> Not that these changes will necessarily fix your program, but they do
> make it easier to comprehend for the reader.
>
> - Chris
>
>>
>>
>> item.endswith() does not seem to be working.
>>
>> Please help
>> --
>> Regrads,
>> Rajat
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
Thanks for the suggestions.
I wondered if the item is really a string. So I added the following to
check this:
item = ""
for item in dirList:
print type(item)
if item.endswith('\\'):
item = item[:-1]
dirListFinal.append(item)
Though item.endswith() is not workin still. The type of item is
appearing to be <type 'str'>
So this confirms this is a string. But why the string operation
endswith() is not working.
Really strange.
More information about the Python-list
mailing list