python中open的newdline参数

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

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值