473,651 Members | 2,566 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ap pend(item)
else:
dirListFinal.ap pend(item)
item.endswith() does not seem to be working.

Please help
--
Regrads,
Rajat
Aug 29 '08 #1
5 15593
On Aug 29, 2:28*pm, dudeja.ra...@gm ail.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] * * * * # Thisone I googled and
found to remove the last character /
* * * * * * * *dirListFinal.a ppend(item)
* * * * * *else:
* * * * * * * *dirListFinal.a ppend(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.
Aug 29 '08 #2
On Aug 29, 1:28*pm, dudeja.ra...@gm ail.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] * * * * # Thisone I googled and
found to remove the last character /
* * * * * * * *dirListFinal.a ppend(item)
* * * * * *else:
* * * * * * * *dirListFinal.a ppend(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
Aug 29 '08 #3
On Fri, Aug 29, 2008 at 7:41 PM, Mike Driscoll <ky******@gmail .comwrote:
On Aug 29, 1:28 pm, dudeja.ra...@gm ail.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.ap pend(item)
else:
dirListFinal.ap pend(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?
Aug 29 '08 #4
On Fri, Aug 29, 2008 at 7:59 PM, <du**********@g mail.comwrote:
On Fri, Aug 29, 2008 at 7:41 PM, Mike Driscoll <ky******@gmail .comwrote:
>On Aug 29, 1:28 pm, dudeja.ra...@gm ail.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:

dirListFina l = []
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.ap pend(item)
else:
dirListFinal.ap pend(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
Aug 29 '08 #5
du**********@gm ail.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>

Aug 29 '08 #6

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

Similar topics

1
36471
by: Olivia Towery | last post by:
How do I delete the last character in a string? Thanks, Olivia Towery
6
2267
by: Carol | last post by:
With ASP, I am generating a huge CSV file. It is to large to open in notepad. Can I use the Filesystem Scripting Object to read the very last line of the file and delete the very last character? I am hoping the DSO will not have to read thru the whole file. it is probably way to big. Thank youi very much
1
1666
by: tshad | last post by:
I am trying to find the best way to delete the last character from a textbox. For example: status.text = "full/part/" I want to take out the trailing "/". Thanks,
2
15438
by: Sharkie | last post by:
I'm relatively new to XSLT, having strong background in bunch of other programming languages (Perl, Java, etc.). I'm writing an XSLT stylesheet, converting XML into HTML. Everything works just fine, except for one detail which I don't know how to handle. I have one element, which sometimes ends with a period, and sometimes not (the value of that element that is). In the output however, I need a period always. If I place a period inside...
6
18400
by: Daniel Mark | last post by:
Hello all: I have the following snippet: In : fileName = 'Perfect Setup.txt\n' In : fileName = fileName # remove the '\n' character In : fileName Out: 'Perfect Setup.txt'
8
80264
by: Johny | last post by:
Let's suppose s='12345 4343 454' How can I replace the last '4' character? I tried string.replace(s,s,'r') where 'r' should replace the last '4'. But it doesn't work. Can anyone explain why? Thanks
4
7383
by: Rasputin | last post by:
Hello, The topic of this question is somewhat related to my previous question, but I wasn't sure if it diserved its own thread. In case of doubt... I double threated. If it's not OK let me know for next time. I am reading strings from a file to a vector, doing some "operations" on them, and then rewriting a new file. I noticed, after intense searching, than when there is a newline ("\n") at the end of the input file, the last...
2
3079
by: Joca | last post by:
This is probably something easy to do, but i'm trying to erase the last character from a string, for example: "Cats" into "Cat" Does anyone have any idea on how to do that? Thanks in advance.
0
1134
by: Fredrik Lundh | last post by:
dudeja.rajat@gmail.com wrote: explicitly comparing against true is bad style; better write that as if item.endswith('\\'): item.endswith("\\") works just fine:
0
8352
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8802
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8697
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...
1
8465
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6158
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
5612
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
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2699
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

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.