Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > remove the last character or the newline character?

Reply
Thread Tools

remove the last character or the newline character?

 
 
Daniel Mark
Guest
Posts: n/a
 
      09-28-2006
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

 
Reply With Quote
 
 
 
 
John Salerno
Guest
Posts: n/a
 
      09-28-2006
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.
 
Reply With Quote
 
 
 
 
Georg Brandl
Guest
Posts: n/a
 
      09-28-2006
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
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      09-28-2006
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 '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      09-28-2006
"Daniel Mark" <(E-Mail Removed)> wrote:


> 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')
 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      09-28-2006
> 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




 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      09-28-2006
>> 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




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace the last (and only last) character in a string? Johny Python 8 05-03-2007 09:24 PM
removing newline character from the buffer read by fgets junky_fellow@yahoo.co.in C Programming 16 11-28-2006 10:44 PM
File reading problem probably newline character utab C++ 1 03-13-2006 04:18 PM
Reverse Newline character? 3strands@gmail.com C++ 6 09-17-2005 05:04 PM
NEWLINE character problem Dragos Chirila Python 1 01-30-2004 03:30 PM



Advertisments