473,657 Members | 2,806 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

du**********@gm ail.com wrote:
On Fri, Aug 29, 2008 at 7:40 PM, Chris Rebert <cv**********@g mail.comwrote:
>On Fri, Aug 29, 2008 at 11:25 AM, <du**********@g mail.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:

dirListFina l = []
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.ap pend(item)
else:
dirListFinal.ap pend(item)
And those last 3 lines are a bit redundant. Just put one

dirListFinal.ap pend(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.ap pend(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 1707

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

Similar topics

8
2523
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
13273
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
32800
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, "-1234.5"); // populate the data At this point, I'd like to remove the '-' that's in the 1st character position (and this example isn't realistic), and I can't see a good way to do this: strcpy with a char pointer that points to the 2nd character;...
3
4012
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 control. This is a WinForms application, not a webform. I have tried... re.SelectionLength = 1; re.SelectionStart = re.Textlength-1; re.SelectionText = ""; // empty string
6
2576
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 during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.IO.PathTooLongException: The path is too long
5
1759
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 = inText.substr(0, outLength); }
1
2331
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 execution or error message, the program puts the cursor on the next line, and waits for the next command. i have two commands that I CANNOT SEEM TO GET TO WORK...addDVD and deleteDVD. I just learned that I can't use an array, because i "must"...
0
795
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, unfortunately this does not work: dirListFinal = for item in dirList:
5
15594
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 from the elements that have this character set at their end,
0
153
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: item = "" for item in dirList: print type(item)
0
8316
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8737
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8610
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6174
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2735
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1730
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.