- When reading input from the stream, if newline is
None
, universal newlines mode is enabled. Lines in the input can end in'\n'
,'\r'
, or'\r\n'
, and these are translated into'\n'
before being returned to the caller. If it is''
, universal newlines mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated. - 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 newlineis any of the other legal values, any'\n'
characters written are translated to the given string.
讀取數據的時候,如果newline參數爲None,那麼universal newlines模式就會被激活。文件中的'\n','\r\n','\r',在返還給調用者的時候都會被轉化爲'\n'。如果newline = '',那麼universal newlines也會被激活,但是行的結尾符號並不會被轉化。(就這句話讓我有點懵逼,讀取的時候不轉化,那麼也會被激活是什麼意思?實例可以如下代碼)
當寫入的時候,如果不設置newline參數,那麼輸入中的任何'\n'都會被翻譯成當前系統的line separator(也就是os.linesep),如果參數爲''或者'\n',不會有任何翻譯。如果是任何其他的合法輸入值,'\n'會被翻譯成相應的值