美文网首页
文件读写

文件读写

作者: 爱吃葡萄冰的鲸 | 来源:发表于2017-10-05 23:48 被阅读5次

转载自:http://blog.csdn.net/pfm685757/article/details/47806469

如果不带newline=‘’,你会发现也能写入结果,但是每行内容之间总是会多出一个空行

下面是python参考手册里的解释,在windows这种使用\r\n的系统里,不用newline=‘’的话,会自动在行尾多添加个\r,导致多出一个空行,即行尾为\r\r\n
In Python 2.X, it was required to open the csvfile with 'b' because the csv module does its own line termination handling.
In Python 3.X, the csv module still does its own line termination handling, but still needs to know an encoding for Unicode strings. The correct way to open a csv file for writing is:

outputfile=open("out.csv",'w',encoding='utf8',newline='') 

encoding can be whatever you require, but newline='' suppresses text mode newline handling. On Windows, failing to do this will write \r\r\n file line endings instead of the correct \r\n. This is mentioned in the 3.X csv.reader documentation only, but csv.writer requires it as well.

相关文章

  • C语言读写文件

    C语言文件读写### 标准文件读写 非标准文件读写 标准文件读写 头文件 include 打开文件 函数原型:FI...

  • 跟我一起学Python(八)

    一、IO编程 读写文件是最常见的IO操作,Python内置了读写文件的函数。文件读写的原理:在磁盘上读写文件的功能...

  • Python 学习笔记6 2018-04-13

    文件操作: 1,文件的读写操作 2,文件的各种系统操作 3,存储对象 1,文件的读写操作 读写数据: ...

  • 用Python实现磁盘IO操作全攻略,让数据流动起来!

    01 文件读写 1. 打开文件 读写文件是最常见的IO操作。Python内置了读写文件的函数,方便了文件的IO操作...

  • 2018-04-05

    文件与文件路径读写文件用shelve模块保存变量 1 python 读写文件 1.1 文件与文件路径 window...

  • 文件操作导航

    文件打开与关闭文件读写文件的定位读写文件的重命名、删除文件夹的相关操作

  • R数据读写

    csv文件读写 txt文件读写 读取excel文件 转成csv文件读取(逗号分隔) 专程prn文件读取(空格分隔)...

  • python学习笔记03

    文件处理 读写文件是最常见的IO操作。Python内置了读写文件的函数,用法和C是兼容的。 读写文件前,我们先必须...

  • python 文件操作

    读写文件通常包含以下操作: 打开文件。获取文件对象 读写文件、对文件内容进行操作。 关闭文件。使用文件对象关闭文件...

  • Python学习_IO文件操作

    在编程工作中,时常需要对各种文件进行操作。读写文件是最常见的IO编程,Python中内置了读写文件的函数。读写文件...

网友评论

      本文标题:文件读写

      本文链接:https://www.haomeiwen.com/subject/wszkyxtx.html