remove the last character or the newline character?

Discussion in 'Python' started by Daniel Mark, Sep 28, 2006.

  1. Daniel Mark

    Daniel Mark Guest

    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
     
    Daniel Mark, Sep 28, 2006
    #1
    1. Advertisements

  2. Daniel Mark

    John Salerno Guest

    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.
    Or you can use lstrip() or rstrip() to remove just the left or right side.
     
    John Salerno, Sep 28, 2006
    #2
    1. Advertisements

  3. Daniel Mark

    Georg Brandl Guest

    fileName = fileName[:-1]
    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
     
    Georg Brandl, Sep 28, 2006
    #3
  4. fileName = fileName.rstrip('\n')
    or just a plain:
    fileName = fileName.strip()
    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()
    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, Sep 28, 2006
    #4
  5. Daniel Mark

    Duncan Booth Guest

    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).
    To remove all trailing whitespace:

    fileName = fileName.rstrip()

    to just remove a newline:

    fileName = fileName.rstrip('\n')
     
    Duncan Booth, Sep 28, 2006
    #5
  6. Daniel Mark

    Tim Chase Guest

    In [1]: fileName = 'Perfect Setup.txt\n'
    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.
    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
     
    Tim Chase, Sep 28, 2006
    #6
  7. Daniel Mark

    Tim Chase Guest

    Does python provide any function that can remove the newline character
    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:
    ' 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
     
    Tim Chase, Sep 28, 2006
    #7
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.