python中的newline_python open函数newline用法

写这些文章主要是督促自己学习,过去两年时间,断断续续在学习linux、python以及机器学习方面的知识,东西看了很多,却没有实际动手操作,很多不明白的知识当时解决后没有记录,现在也忘得差不多了。为此,希望能在这里记录自己每天的学习轨迹,以后温习时也方便。

1.不同操作系统换行符不统一

linux:\n    windows:\r\n    mac:\r

2.open函数newline用法If csvfile is a file object, it should be opened with newline=''.

上述引用来自python 中关于csv标准库的介绍,对于这句话相当疑惑,因此,编写以下程序来辨别。

Case 1: The file is read and written with newline=''.csvfile=open('csvfile.csv','w',newline='')

writer=csv.writer(csvfile)

writer.writerow('a')

writer.writerow('b')

csvfile.close()

csvfile=open('csvfile.csv','r',newline='')

txtdata=csvfile.read()

csvfile.close()

最终,txtdata中的内容为'a\r\nb\r\n'。

Case 2: The file is written with newline='', but read without it.csvfile=open('csvfile.csv','w',newline='')

writer=csv.writer(csvfile)

writer.writerow('a')

writer.writerow('b')

csvfile.close()

csvfile=open('csvfile.csv','r')

txtdata=csvfile.read()

csvfile.close()

最终,txtdata中的内容为'a\r\nb\r\n'。

Case 3: The file is written without newline='', but read with it.csvfile=open('csvfile.csv','w')

writer=csv.writer(csvfile)

writer.writerow('a')

writer.writerow('b')

csvfile.close()

csvfile=open('csvfile.csv','r',newline='')

txtdata=csvfile.read()

csvfile.close()

最终,txtdata中的内容为'a\r\r\nb\r\r\n'。

Case 4: The file is read and written without newline=''.csvfile=open('csvfile.csv','w')

writer=csv.writer(csvfile)

writer.writerow('a')

writer.writerow('b')

csvfile.close()

csvfile=open('csvfile.csv','r')

txtdata=csvfile.read()

csvfile.close()

最终,txtdata中的内容为'a\n\nb\n\n'。

原因分析On input,if newlineis 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 newline 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.

On output,if newlineis None, any '\n' characters written are translated to the system default line separator,os.linesep. If newlineis '', no translation takes place. If new lineis any of the other legal values,any '\n' characters written are translated to the given string.

csv标准库中的writerow在写入文件时会加入'\r\n'作为换行符,if newline is '',换行符不会被转化而是直接输出,如case 1所示。

当写文件时newline='',程序写入'a\r\nb\r\n';读取文件时newline=None,universal newlines mode工作,换行符'\r\n'被翻译为'\n',如case 2所示。

当写文件时newline=None,csv先是将'a\r\nb\r\n'写入内存,再写入文件时,universal newlines mode工作,换行符'\n'被翻译为'\r\n',最终结果如case 3所示。

当写文件时newline=None,csv先是将'a\r\nb\r\n'写入内存,再写入文件时,universal newlines mode工作,换行符'\n'被翻译为'\r\n';读取文件时newline=None,universal newlines mode工作,换行符'\r'和'\r\n'被翻译为'\n',显示为'a\n\nb\n\n',如case 4所示。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值