def remove_newline(string, n=1):
    """Remove up to `n` trailing newlines from `string`."""
    for _ in range(n):
        if string.endswith('\r\n') or string.endswith('\n\r'):
            string = string[:-2]
        elif string.endswith('\n') or string.endswith('\r'):
            string = string[:-1]
        else:
            break
    return string
