(Python 3.5.1)
I'm trying to make an EOL character "normalizer", in that it will take a file and change the EOL characters to ones that the user specifies.
The problem is that I think python is treating '\n' as the native EOL chars. For example, I am on Windows which uses CRLF, so it's writing '\n' as '\r\n' to the file. (I used HxD to confirm this)
Here's my code:
for line in file_in:
line = line.rstrip("\r\n") #Strip EOL chars
line += "\n" #Add normalized EOL char
file_out.write(line)
Is there any way to make python take '\n' as LF instead of replacing it with the native EOL chars?
If you open the file in binary mode (e.g., file_out = open('somefile.txt', 'wb')
), it will only write out the bytes you give it, without doing newline translation. You'll probably also want to open your input file file in binary mode to make sure it won't do newline translation when reading it in.