473,395 Members | 1,452 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,395 software developers and data experts.

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

Please help
--
Regrads,
Rajat
Aug 29 '08 #1
5 15577
On Aug 29, 2:28*pm, dudeja.ra...@gmail.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.append(item)
* * * * * *else:
* * * * * * * *dirListFinal.append(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...@gmail.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.append(item)
* * * * * *else:
* * * * * * * *dirListFinal.append(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...@gmail.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.append(item)
else:
dirListFinal.append(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**********@gmail.comwrote:
On Fri, Aug 29, 2008 at 7:41 PM, Mike Driscoll <ky******@gmail.comwrote:
>On Aug 29, 1:28 pm, dudeja.ra...@gmail.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.append(item)
else:
dirListFinal.append(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**********@gmail.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
by: Olivia Towery | last post by:
How do I delete the last character in a string? Thanks, Olivia Towery
6
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...
1
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
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...
6
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
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?...
4
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...
2
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
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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...
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.