Python Forum

Full Version: strip off just newlines
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
so i have a string that might have multiple newlines at the end, or maybe a carriage-return line-feed pair. but i want the string with all of them removed. here's how i did it:
    line = line.splitlines()[0]
There is also str.rstrip()
Hi,

Quote: here's how i did it:
But this way gives you a list as a result, not string... So either you need to join the list to a sting again or use the replacemethod of strings.

Regards, noisefloor
(Jun-17-2019, 10:20 AM)noisefloor Wrote: [ -> ]Hi,

Quote: here's how i did it:
But this way gives you a list as a result, not string... So either you need to join the list to a sting again or use the replacemethod of strings.

Regards, noisefloor

It gives list but there is index used so the result is string.
first, *rest = map(str.rstrip, line.splitlines())
This won't fail if you have only one line. Then the rest is an empty list.
If there are 0 lines, it will raise a ValueError, because there are not enough values (1) to unpack.

If you do this:
first = map(str.rstrip, line.splitlines())
Then first is an lazy evaluated map Iterator, not a sequence.

The top one works, because there is argument unpacking going on
and if this is the case, the iterable on the right side is consumed.
(Jun-17-2019, 07:19 AM)perfringo Wrote: [ -> ]There is also str.rstrip()
str.rstrip() also removes trailing white spaces. while that is often good to do, there are some things that it is not right for. can you show the characters argument for str.rstrip() to get it to work the way i described?
>>> help(str.rstrip)
Help on method_descriptor:

rstrip(self, chars=None, /)
    Return a copy of the string with trailing whitespace removed.
    
    If chars is given and not None, remove characters in chars instead.
(END)
>>> s = 'space and newline at end \n'
>>> s.rstrip('\n')
'space and newline at end '
>>> s = 'abc \n\n'
>>> s.rstrip('\n')
'abc '
it might have spaces at the end that i want to keep.
(Jun-18-2019, 05:04 AM)Skaperen Wrote: [ -> ]it might have spaces at the end that i want to keep.

If there is space at end then original code will not handle them as well (splits on newline and takes only stuff before newline), so .rstrip() is in parity by functionality.

In case of removing some characters in arbitrary/all locations then str.replace and str.translate might be considered:

>>> help(str.replace)
Help on method_descriptor:

replace(self, old, new, count=-1, /)
    Return a copy with all occurrences of substring old replaced by new.
    
      count
        Maximum number of occurrences to replace.
        -1 (the default value) means replace all occurrences.
    
    If the optional argument count is given, only the first count occurrences are
    replaced.
(END)
>>> help(str.translate)
Help on method_descriptor:

translate(self, table, /)
    Replace each character in the string using the given translation table.
    
      table
        Translation table, which must be a mapping of Unicode ordinals to
        Unicode ordinals, strings, or None.
    
    The table must implement lookup/indexing via __getitem__, for instance a
    dictionary or list.  If this operation raises LookupError, the character is
    left untouched.  Characters mapped to None are deleted.
(END)
if i have 'foobar \t\r\n' or 'foobar \t\n\n\n' then i want to end up with 'foobar \t'. if it fails because there is no newline then the previous code deal with it. my original use if this was in a program doing changes to a file.
    for line in stdin:
        line = line.splitlines()[0]
        ...
but i really should better handle the case of the last line of the file having end of file without a newline. if .splitlines() raises an exception in that case, then oops. but .rstrip() didn't preserve the ' \t' at the end of the line. where input has '\r\n' the output can just have '\n' which print() adds by default. but, maybe i should keep whatever newline the innput file has. i still need to process each line.
Pages: 1 2