472,353 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

remove the last character or the newline character?

Hello all:

I have the following snippet:

In [1]: fileName = 'Perfect Setup.txt\n'
In [2]: fileName = fileName[0:len(fileName)-1)] # remove the '\n'
character
In [3]: fileName
Out[3]: 'Perfect Setup.txt'

Question one:
Does python provide any function that can remove the last character of
a string?
I don't know whether or not the method I used is efficient

Question two:
Does python provide any function that can remove the newline character
from a string if it exists?

Thank very much!
-Daniel

Sep 28 '06 #1
6 18194
Daniel Mark wrote:
Question one:
Does python provide any function that can remove the last character of
a string?
I don't know whether or not the method I used is efficient
Doing fileName[-1] lets you access the last character, but I'm not sure
about removing it since strings are immutable. It would still probably
be something like you did.
Question two:
Does python provide any function that can remove the newline character
from a string if it exists?
>>fileName = 'Perfect Setup.txt\n'
fileName.strip()
'Perfect Setup.txt'
>>>
Or you can use lstrip() or rstrip() to remove just the left or right side.
Sep 28 '06 #2
Daniel Mark wrote:
Hello all:

I have the following snippet:

In [1]: fileName = 'Perfect Setup.txt\n'
In [2]: fileName = fileName[0:len(fileName)-1)] # remove the '\n'
character
In [3]: fileName
Out[3]: 'Perfect Setup.txt'

Question one:
Does python provide any function that can remove the last character of
a string?
I don't know whether or not the method I used is efficient
fileName = fileName[:-1]
Question two:
Does python provide any function that can remove the newline character
from a string if it exists?
fileName = fileName.rstrip("\n")

though this will remove more than one newline if present. If you only want
to remove one newline, use

if fileName[-1:] == '\n':
fileName = fileName[:-1]

Georg
Sep 28 '06 #3
In [1]: fileName = 'Perfect Setup.txt\n'
In [2]: fileName = fileName[0:len(fileName)-1)] # remove the '\n'
character
In [3]: fileName
Out[3]: 'Perfect Setup.txt'

Question one:
Does python provide any function that can remove the last character of
a string?
I don't know whether or not the method I used is efficient
You're close...

fileName = fileName[0:-1]

which is the same as

fileName = fileName[:-1]

which will lop off the last character. Much nicer than most
other languages I've used where you have to use the len() trick
you're using. Also, it's a common python idiom you'll see
frequently.
Question two:
Does python provide any function that can remove the newline character
from a string if it exists?
In a discussion on this very matter a while back, I think the
final verdict was something like

fileName = fileName.rstrip('\n')

which will strip off *all* the trailing newlines. In most cases
(such as "for line in file('foo.txt'):" code), there's only one
to be stripped, so this works fine.

If you're ornary and want *only* the last '\n' lopped off, you'd
have to test for it:

if fileName[-1] == '\n': fileName = fileName[:-1]

There were caveats regarding "\n" vs. "\r\n" line endings, but if
the file comes in in ascii mode (rather than binary mode), Python
more or less smart enough to do the translation for you, leaving
the above code to work in the majority of cases.

-tkc


Sep 28 '06 #4
Daniel Mark wrote:
Hello all:

I have the following snippet:

In [1]: fileName = 'Perfect Setup.txt\n'
In [2]: fileName = fileName[0:len(fileName)-1)] # remove the '\n'
fileName = fileName.rstrip('\n')
or just a plain:
fileName = fileName.strip()
character
In [3]: fileName
Out[3]: 'Perfect Setup.txt'

Question one:
Does python provide any function that can remove the last character of
a string?
Not directly, since Python strings are immutable objects. If you want a
copy of the string without the last char *whatever it is*, you can just use
somestr = somestr[0:-1]

But in your situation, it's usually safer to use [lr]?strip()
>
Question two:
Does python provide any function that can remove the newline character
from a string if it exists?
Here again, you cannot *remove* anything from a string - you can just
have a modified copy copy of the string. (NB : answer is just above :
use str.strip())

HTH

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Sep 28 '06 #5
"Daniel Mark" <da***********@gmail.comwrote:

In [2]: fileName = fileName[0:len(fileName)-1)] # remove the '\n'
To chop the last character regardless of what it is:

fileName = fileName[:-1]

You don't need the 0 since thats the default, and you can use a negative
index instead of subtracting from len(x).
Question two:
Does python provide any function that can remove the newline character
from a string if it exists?
To remove all trailing whitespace:

fileName = fileName.rstrip()

to just remove a newline:

fileName = fileName.rstrip('\n')
Sep 28 '06 #6
>Does python provide any function that can remove the newline character
>from a string if it exists?
>>fileName = 'Perfect Setup.txt\n'
>>fileName.strip()
'Perfect Setup.txt'
>>>

Or you can use lstrip() or rstrip() to remove just the left or right side.
Just a caveat with the non-qualified strip/rstrip/lstrip...it
will remove *all* whitespace, so if, for some reason, it's
significant, and you only want to remove newlines, you have to
specify it:
>>s = ' some text \t\n'
s.strip()
'some text'
>>s.rstrip()
' some text'
>>s.rstrip('\n')
' some text \t'

As the OP was talking about file-names, the use of
initial/terminal spaces is allowable (albeit imprudent),
depending on your platform, so one may or may not want to strip
them off.

Fortunately, as in many other areas, Python offers the
flexibility in an easy-to-use way, and comes with sensible defaults.

-tkc


Sep 28 '06 #7

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

Similar topics

1
by: utab | last post by:
Hi there I am trying to read from a file, I am trying to read certain fields,there are 6 fields in this file like ...
13
by: comp.lang.php | last post by:
Other: <input name="school_type_other" size="30" maxlength="75" value="<?php if ($_POST) echo $_POST; else echo str_replace('"', '&quot;',...
0
by: Adam Right | last post by:
Hi, I am using .net framework functions to send a mail over exchange server but i want to use newline character in the body. I cannot insert this...
5
by: Adam Right | last post by:
Hi, Is there a way to construct the mail body including newline characters by using .net framework mailing functions when sending an email? I...
16
by: junky_fellow | last post by:
Is there any efficcient way of removing the newline character from the buffer read by fgets() ? Is there any library function that is similar to...
1
by: toefraz | last post by:
Hello, all. I am writing a patch of code that displays the time and date in the middle of the line. I am using the asctime function from ctime but I...
3
by: skris | last post by:
Hi All, I have some contents in a file named Msg.txt. I need to append the date right after the end of every line of that file. I tried out this...
0
by: Gary Herron | last post by:
Support Desk wrote: The problem has nothing to do with lists. The readlines() function returns each line *with* its newline. To strip it off,...
16
by: bgold12 | last post by:
Will newlines ever be standardized? I recently discovered that in a textarea, internet explorer adds \r\n for every newline you enter, while...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...

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.