Reputation: 3249
Is there a way to replace all types of the character newline in python by "\n"? The most common newline characters seem to be "\n" and "\r" but in wikepedia you can find different representations. I am looking for something like:
For whitespaces (using re):
txt = re.sub(r'[\s]+',' ',txt)
For hyphens (using regex).. See reference here:
txt = regex.sub(r'\p{Pd}+', '-', txt)
Upvotes: 2
Views: 1838
Reputation: 4967
To replace any \r
(carriage return) by \n
(new line) :
txt = re.sub(r"\r", "\n", txt)
r
before double quote means raw string to escape the slash.
Upvotes: 1
Reputation: 626690
There is a \R
construct that you may use in Python PyPi regex module. However, even with re
, you may use its equivalent:
re.sub(r'\u000D\u000A|[\u000A\u000B\u000C\u000D\u0085\u2028\u2029]', '\n', s)
See the Python demo.
Upvotes: 5