473,396 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Re: How to delete a ast character from a string?

du**********@gmail.com wrote:
On Fri, Aug 29, 2008 at 7:40 PM, Chris Rebert <cv**********@gmail.comwrote:
>On Fri, Aug 29, 2008 at 11:25 AM, <du**********@gmail.comwrote:
>>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
no******@hughes.net
Aug 30 '08 #1
0 1672

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Randy Gordon | last post by:
Say I do the following: char **names = new char*; for (int i = 0; i < 100; i++) { names = new char; } When I'm done with the array of character pointers, how should I delete it? Like this:
5
by: Raj | last post by:
Hi all, Can anyone help me with a script which would delete files or move them to a different folder at some scheduled time..! Please.....!!! Thanks in advance...
8
by: Michael R. Copeland | last post by:
What is a good way to delete 1 character (or several) from a character array that's being processed as a "string"? Specifically, I have something like: char szWork; .... strcpy(szWork,...
3
by: ozbear | last post by:
The title says it all. Since a Richedit control doesn't understand a backspace character ('\b', or 0x08) when I receive one I need to programmatically delete the last character from the Richedit...
6
by: I am Sam | last post by:
I keep getting this error and I don't know why: The path is too long after being fully qualified. Make sure path is less than 260 characters. Description: An unhandled exception occurred...
5
by: michael | last post by:
Hi All, I have the following: const int LENGTH = 5; void limitNameLength(string inText, char *&outText, int outLength){ if(static_cast<int>(inText.length()) outLength){ inText =...
1
by: jmarcrum | last post by:
Hello all! i have a “monitor-type” program, where my program recognizes my defined "commands”, and executes the given command, or produces an error message that says “unrecognized command”. After...
0
by: dudeja.rajat | last post by:
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,...
5
by: dudeja.rajat | last post by:
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...
0
by: dudeja.rajat | last post by:
On Fri, Aug 29, 2008 at 7:40 PM, Chris Rebert <cvrebert+clp@gmail.comwrote: Thanks for the suggestions. I wondered if the item is really a string. So I added the following to check this: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.