I have a python script that gave different output when run on a Windows machine and when run on a Mac. On digging deeper, I discovered that it was because when Python read in line breaks on the Mac (from a file), it read in \r\n
, while somehow in Windows the \r
disappears.
Thus, if I change every \n
in the script to \r\n
, it works fine on the Mac. But if I do that, it stops working on the Windows PC.
Is there an easy way to fix this problem?
In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.
With triple-quoted string literals, you can use actual newlines instead of \n . print(""" 99 buckets of bits on the bus.
The new line character in Python is \n . It is used to indicate the end of a line of text.
When writing output to the stream, if newline is None, any '\n' characters written are translated to the system default line separator, os. linesep. If newline is '' or '\n', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string.
Different platforms have different codes for "new line". Windows have \r\n, Unix has \n, Old macs have \r and yes there are some systems that have \n\r too.
When you open a file in text mode in Python 3, it will convert all newlines to '\n' and be done with it.
infile = open("filename", 'r')
Text mode is default, so if you say nothing, it's text mode. But it's always better to be explicit:
infile = open("filename", 'rt')
If you don't want the translation of line endings to happen, open the file in binary mode:
infile = open("filename", 'rb')
In Python 2 it's different. There this conversion would only happen by default on Windows. If you wanted it to happen on other platforms, you could add the universal newline flag:
infile = open("filename", 'rU')
However, you say that you are on Python 3, and there it happens in text mode on all platforms, so adding the U flag should make no difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With