Python Forum
conversion of newlines in a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
conversion of newlines in a string
#1
what is the best function to convert newlines in a string to Unix style whether in Windows style or Mac style (or a mix of both), all in one function call? i could do this in C in a way that would not be good in Python (picking at characters one by one).
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
I don't know if it is the best solution but you could try
import re
new_string = re.sub(rb'\r\n?', b'\n', old_string)
A potential problem is the case of a Windows file which contains \r characters not followed by \n. These \r characters will be interpreted as new lines because we don't know a priori if it is a Windows file or a Mac file.
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
>>> foo = 'spam\reggs\nspam\r\neggs'
>>> '\n'.join(foo.splitlines())
'spam\neggs\nspam\neggs'
docs for str.splitlines()
Gribouillis likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(Jan-14-2024, 08:39 AM)Gribouillis Wrote: because we don't know a priori if it is a Windows file or a Mac file.
so, \r has a different meaning depending on which. i recall that, now. i was thinking in a narrow box trying to cover potential cases. i will know a priori whether it is Windows or Mac so i could use a different function for each. i was wondering if i should implement my own set of functions or use what Python might have.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020