Re: remove the last character or the newline character?



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




.



Relevant Pages

  • chapter3
    ... An Informal Introduction to Python ... the hash character, "#", and extend to the end of the physical line. ... string literal is just a hash character. ... Unicode Strings ...
    (Ubuntu)
  • Re: UTF-8 / German, Scandinavian letters - is it really this difficult?? Linux & Windows XP
    ... > If I have this in the beginning of my Python script in Linux: ... > in strings and in Tk GUI button labels and GUI window titles and in ... effect on byte string literals. ... DIAERESIS can be encoded in many different character sets, ...
    (comp.lang.python)
  • Re: Problem with sets and Unicode strings
    ... So this is a bug in Python? ... > statement directly compares a unicode string with a byte string. ... If you put character U+00E4 into a unicode string python ... the ASCII codec, because my file is encoded with UTF-8. ...
    (comp.lang.python)
  • Re: Why tuple with one item is no tuple
    ... Complaints about typing an extra character are not all the same. ... >> no way to have both operators in a natural way in python. ... > have mutable keys for dicts that needed copying the very same keys - at ...
    (comp.lang.python)
  • Re: Decorator syntax
    ... DEF UPDATE_COUNT: ... It seems to me that historically Python has eschewed using special ... invoking a special punctuation character, it uses context and placement, ...
    (comp.lang.python)