Given a text-string of unknown source, how does one best rewrite it to have a known lineend-convention?
I usually do:
lines = text.splitlines()
text = '\n'.join(lines)
... but this doesn't handle "mixed" text-files of utterly confused conventions (Yes, they still exist!).
Edit
The oneliner of what I'm doing is of course:
'\n'.join(text.splitlines())
... that's not what I'm asking about.
The total number of lines should be the same afterwards, so no stripping of empty lines.
Testcases
Splitting
'a\nb\n\nc\nd'
'a\r\nb\r\n\r\nc\r\nd'
'a\rb\r\rc\rd'
'a\rb\n\rc\rd'
'a\rb\r\nc\nd'
'a\nb\r\nc\rd'
.. should all result in 5 lines. In a mixed context, splitlines assumes that '\r\n' is a single logical newline, leading to 4 lines for the last two testcases.
Hm, a mixed context that contains '\r\n' can be detected by comparing the result of splitlines() and split('\n'), and/or split('\r')...