How to delete a ast character from a string?

norseman norseman at hughes.net
Fri Aug 29 20:28:42 EDT 2008


dudeja.rajat at gmail.com wrote:
> 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.
> --
> http://mail.python.org/mailman/listinfo/python-list
> 

==============================
depending on OS and other factors, the DirList may be more like:

ABDIR\  41 42 44 49 52 5C 0A
nextDir

If so endswith needs to look for \\ and \n
or just
if item[:-2] == '\x5C\x0A':
   item=item[:-2]

Best to dump a test line to hex and see what you are actually dealing 
with.  This is nearly a daily thing for me.

EOL  Microsoft  0D0A
EOL  Unix         0A
EOL  Mac        0D
and the programmers that wrote the read and /or write routines vary 
widely in their handling of EOLs.  Some remember to strip the 0A but 
leave the 0D and some do the opposite  and some just add/remove 2 bytes 
without checking.  Never know what you are going to wind up with. If 
your files are from varied sources, put a test for each in the front of 
the section and set/use variables to control how many things you need to 
remove. (-1,-2 or -3  for 0,1 or 2 EOL bytes respectively since you seem 
to be removing the continuation marker)

Depending on what you used to acquire the list be advised that Python 
has routines that cook the data. Not all but some. So that line just 
read may not contain what it appears to. Examples of this have appeared 
in this mail list recently.  Choose your routines carefully.

It is very easy to get into a singular mindset and not consider the rest 
of the Universe.  We all do it.  If you want purity of form you will 
have to sacrifice efficiency. Also true in reverse.


Steve
norseman at hughes.net



More information about the Python-list mailing list