def collapse_linebreaks(string, threshold=1):
 n = ('\n' * threshold)
 p = [s.rstrip() for s in string.splitlines()]
 string = '\n'.join(p)
 string = re.sub((n + '+'), n, string)
 return string

def collapse_linebreaks ( string , threshold = 1 ) : n = ( '\n' * threshold ) p = [ s . rstrip ( ) for s in string . splitlines ( ) ] string = '\n' . join ( p ) string = re . sub ( ( n + '+' ) , n , string ) return string
replace consecutive crs and/or lfs with single crlfs .