Posts: 4,646
Threads: 1,493
Joined: Sep 2016
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.
Posts: 4,787
Threads: 76
Joined: Jan 2018
Jan-14-2024, 08:39 AM
(This post was last modified: Jan-14-2024, 08:39 AM by Gribouillis.)
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 »
Posts: 4,646
Threads: 1,493
Joined: Sep 2016
(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.