python tips:文件读取——换行符的问题
问题:在windows系统中,换行的符号是'\r\n'。python在读文件的时候为了系统兼容,会默认把'\r','n','\r\n'都视作换行。但是在windows文件中,可能在同一行中同时存在'\n','\r\n','\r'。这个时候python的默认行为会将一行拆分成多行输出,影响预期结果。
此时需要设置open函数的newline参数,修改python对换行的默认行为。
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
newline有五种取值:None,'','\n','\r','\r\n'。
在输入过程(从文件到程序),newline用于定义换行的符号:
1.如果newline为None,碰到'\r','\n','\r\n'都算行尾,而且这些符号都会被转换成'\n'。
2.如果newline为'',也是碰到'\r','\n','\r\n'都算行尾,但是这些符号不会发生转换。
3.如果newline为'\r','\n','\r\n',等于是显示指定了换行符,而且行中的符号不会发生转换。
在输出过程(从程序到文件),newline用于指定'\n'的转换符号:
1.如果newline为None,所有的'\n'都被转换成系统换行符。
2.如果newline为'','\n',不会发生转换。
3.如果newline为'\r','\r\n',所有的'\n'会被转换成'\r'或者'\r\n'。
实例一:输出不指定newline,所有的'\n'都被替换成'\r\n',即使是'\r\n'中的'\n'也不例外。
def file_seperator_test1():
# output
with open("medical.txt", "w") as f:
f.write("I am a\r good\n boy.\r\n")
#input
with open("medical.txt", "r", newline="\r\n") as f:
print(list(f)) if __name__ == "__main__":
file_seperator_test1()
输出结果:
['I am a\r good\r\n', ' boy.\r\r\n']
实例二: 输出指定newline为''或'\n',不会转换
def file_seperator_test2():
# output
with open("medical.txt", "w", newline="") as f:
f.write("I am a\r good\n boy.\r\n")
with open("medical2.txt", "w", newline="\n") as f:
f.write("I am a\r good\n boy.\r\n") #input
with open("medical.txt", "r", newline="\r\n") as f:
print(list(f))
with open("medical2.txt", "r", newline="\r\n") as f:
print(list(f)) if __name__ == "__main__":
file_seperator_test2()
输出结果:
['I am a\r good\n boy.\r\n']
['I am a\r good\n boy.\r\n']
实例三:输出指定newline为'\r'或'\r\n',所有的'\n'都被替换了,当所有'\n'都被替换成'\r'时,在windows中,换行符就不见了,所有的行变成了一行
def file_seperator_test3():
# output
with open("medical.txt", "w", newline="\r") as f:
f.write("I am a\r good\n boy.\r\n where should\r\n I change the line ?\r\n")
f.write("I can't stop\r\n")
with open("medical2.txt", "w", newline="\r\n") as f:
f.write("I am a\r good\n boy.\r\n") #input
with open("medical.txt", "r", newline="\r\n") as f:
print(list(f))
with open("medical2.txt", "r", newline="\r\n") as f:
print(list(f)) if __name__ == "__main__":
file_seperator_test3()
输出结果:
["I am a\r good\r boy.\r\r where should\r\r I change the line ?\r\rI can't stop\r\r"]
['I am a\r good\r\n', ' boy.\r\r\n']
实例四:输入不指定newline,默认把所有的三种符号都当做换行符,而且全都转换成'\n'
def file_seperator_test4():
# output
with open("medical.txt", "w", newline="") as f:
f.write("I am a\r good\n boy.\r\n")
#input
with open("medical.txt", "r") as f:
print(list(f)) if __name__ == "__main__":
file_seperator_test4()
输出结果:
['I am a\n', ' good\n', ' boy.\n']
实例五:输入指定newline为'',仍然把三种符号都当做换行符,但是不转换
def file_seperator_test5():
# output
with open("medical.txt", "w", newline="") as f:
f.write("I am a\r good\n boy.\r\n")
#input
with open("medical.txt", "r", newline="") as f:
print(list(f)) if __name__ == "__main__":
file_seperator_test5()
输出结果:
['I am a\r', ' good\n', ' boy.\r\n']
实例六:输入指定newline为'\r','\n','\r\n',显式指定了换行符,只有碰到这几个符号才会换行
def file_seperator_test6():
# output
with open("medical.txt", "w", newline="") as f:
f.write("I am a\r good\n boy.\r\n where should\r\n I change the line ?\r\n")
f.write("I can't stop\r\n")
with open("medical2.txt", "w", newline="") as f:
f.write("I am a\r good\n boy.\r\n where should\r\n I change the line ?\r\n")
f.write("I can't stop\r\n")
with open("medical3.txt", "w", newline="") as f:
f.write("I am a\r good\n boy.\r\n where should\r\n I change the line ?\r\n")
f.write("I can't stop\r\n") #input
with open("medical.txt", "r", newline="\r") as f:
print(list(f))
with open("medical2.txt", "r", newline="\n") as f:
print(list(f))
with open("medical3.txt", "r", newline="\r\n") as f:
print(list(f)) if __name__ == "__main__":
file_seperator_test6()
输出结果:
['I am a\r', ' good\n boy.\r', '\n where should\r', '\n I change the line ?\r', "\nI can't stop\r", '\n']
['I am a\r good\n', ' boy.\r\n', ' where should\r\n', ' I change the line ?\r\n', "I can't stop\r\n"]
['I am a\r good\n boy.\r\n', ' where should\r\n', ' I change the line ?\r\n', "I can't stop\r\n"]
结论:
1.如果要写入带'\n'的行,可以把newline设定为''或者'\n',避免python更改'\n'
2.如果要读入带'\n'的行,可以把newline设定为'\r\n',指定换行符只能是'\r\n'。
python tips:文件读取——换行符的问题的更多相关文章
- python处理文件的换行符
我们知道在Windows平台下的换行符是\r\n,而在linux下的换行符是\n.现在写一个简单程序来测试python是如何处理这些换行符的. 准备文件data.txt,该文件在Windows平台下编 ...
- Python编码/文件读取/多线程
Python编码/文件读取/多线程 个人笔记~~记录才有成长 编码/文件读取/多线程 编码 常用的一般是gbk.utf-8,而在python中字符串一般是用Unicode来操作,这样才能按照单个字 ...
- python大文件读取
python大文件读取 https://stackoverflow.com/questions/8009882/how-to-read-a-large-file-line-by-line-in-pyt ...
- python写文件无法换行的问题
python写文件无法换行的问题,用'\n' 不行,直接打印的出来了. 网上查了查,都说是用 ‘\r\n’ ,但是这样打出来,不仅换行了,还加了一个空行. windows平台最后结果是 直接 ...
- C语言中以文本方式读写文件时换行符转换的注意事项
我们知道在UNIX下是没有回车符(\r)的,只有换行符(\n),而C语言诞生于UNIX(Linux即面向开源的UNIX,Mac OS也是UNIX发展而来的,而Windows是从MS-DOS发展而来,与 ...
- Linux文件和windows文件在 换行符的区别
Linux或Unix文件,和windows文件,在来回处理时,如果不注意 换行符的区别,可能导致程序错误!!!深刻的教训.... 在早期的打印机时代,开始新的一行要占用两个字符的时间.如果到了一行的结 ...
- ****使用ftp软件上传下载php文件时换行符丢失bug
在使用ftp软件上传下载php源文件时,我们偶尔会发现在本地windows下notepad++编辑器写好的php文件,在使用ftp上传到linux服务器后,php文件的换行符全部丢失了,导致php文件 ...
- 记一次错误排查,主要问题是跨平台文件中换行符(CRLF, LF)和垃圾字符( Caret Notation)
笔者测试SPEC14的workload的时候,需要自定义workload,又需要在Windows和Linux平台上都要测试,所以就遇到了这么个问题:测试工具报错,但是报出来的错误信息又跟错误对不上. ...
- 【Python】使用制表符换行符来添加空白
在编程中,在打印时,有时候需要显示出来的数据看着舒服一点,那么使用制表符(\t).换行符(\n)即可轻松实现 >>> print('zhangsan')zhangsan 加入制表符后 ...
随机推荐
- CSS学习笔记之框模型
1.概述 为了更好的处理 元素内容.内边距.边框 和 外边距 之间的关系,CSS 定义了框模型如下: 内边距.边框 和 外边距 的默认值都是零,可以通过设置元素的 padding.border 和 m ...
- HDU4565 So Easy!
/* HDU4565 So Easy! http://acm.hdu.edu.cn/showproblem.php?pid=4565 数论 快速幂 矩阵快速幂 题意:求[(a+sqrt(b))^n ] ...
- HDU 3934
/*这是用的有旋转卡壳的思想. 首先确定i,j,对k进行循环,知道找到第一个k使得cross(i,j,k)>cross(i,j,k+1),如果k==i进入下一次循环. 对j,k进行旋转,每次循环 ...
- plsql developer ini
plsql developer ini [Colors] GradientEnabled=True VerticalGradient=True DefaultGradient=True Gradien ...
- POJ 2762 Going from u to v or from v to u?(强联通,拓扑排序)
id=2762">http://poj.org/problem?id=2762 Going from u to v or from v to u? Time Limit: 2000MS ...
- 使用Lucene对预处理后的文档进行创建索引(可执行)
时间: 2015/3/18 杨鑫newlife 对于文档的预处理后.就要開始使用Lucene来处理相关的内容了. 这里使用的Lucene的过程例如以下: 首先要为处理对象机那里索引 二是构建查询对象 ...
- jstl 标签 循环 序号
大家好: 今天搜了一天.最终找到它了,尽管不是我想要的,可是为了我辛苦的一天.我也要记录下: <c:forEach items="${signBusList}" var ...
- shp系列(六)——利用C++进行Dbf文件的写(创建)
上一篇介绍了shp文件的创建,接下来介绍dbf的创建. 推荐结合读取dbf的博客一起看! 推荐结合读取dbf的博客一起看! 推荐结合读取dbf的博客一起看! 1.Dbf头文件的创建 Dbf头文件的结构 ...
- MongoDB: The Definitive Guide
第一章 简介 MongoDB是面向文档的数据库,不是关系型数据库.内置对MapReduce的支持,以及对地理空间索引的支持. 丰富的数据模型 容易扩展,它所采用的面向文档的数据模型可以使其在多台服务器 ...
- (转)Webpack2 + Vue2 + Vue-Router2 如何实现懒加载?
webpack2 的中 System.import 方法将被弃用, 推荐改成以下写法: https://www.mmxiaowu.com/article/5848239bd4352863efb5546 ...