Jun-19-2019, 09:40 AM
(Jun-18-2019, 06:02 PM)Skaperen Wrote: if i have 'foobar \t\r\n' or 'foobar \t\n\n\n' then i want to end up with 'foobar \t'.
One can chain strip operations. However, it is error prone as depends on order:
>>> first = 'foobar \t\r\n' >>> first.rstrip('\n').rstrip('\r') >>> 'foobar \t' >>> second = 'foobar \t\n\n\n' >>> second.rstrip('\n').rstrip('\r') >>> 'foobar \t' >>> third = 'foobar' >>> third.rstrip('\n').rstrip('\r') 'foobar' >>> fourth = 'foobar \n\r' >>> fourth.rstrip('\n').rstrip('\r') >>> 'foobar \n'But if there are only two strips needed then one can do something 'clever' like this:
>>> fourth = 'foobar \t\n\r' >>> cutouts = ('\n', '\r') >>> last = fourth.endswith('\r') >>> previous = not last >>> fourth.rstrip(cutouts[last]).rstrip(cutouts[previous]) 'foobar \t' >>> fifth = 'foobar \t\r\n' >>> last = fifth.endswith('\r') >>> previous = not last >>> fourth.rstrip(cutouts[last]).rstrip(cutouts[previous]) 'foobar \t'It will not help in mixed case (\r\n\r\n).
After playing with code it seems to me that str.replace is more suitable for task at hand.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.