python中open的readline参数

  • 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'会被翻译成相应的值

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章