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 加入制表符后 ...
随机推荐
- 论vue项目api相关代码的组织方式
论vue项目api相关代码的组织方式 看了下项目组同事的代码,发现不同项目有不同的组织版本 版本一: ├─apis │ a.api.js │ b.api.js │ b.api.js │ d.api.j ...
- ASP.NET Core多平台部署 (Windows Server+IIS与CentOS 7+Nginx)
一,Windows Server+IIS部署 1,安装配置IIS,这个应该都不用多说了,教程一堆 2,下载安装.NET Core Runtime 与 .NET Core SDK,下载请点击下载地址,如 ...
- 简述synchronized和java.util.concurrent.locks.Lock的异同
1.synchronized 用在方法和代码块的区别? a. 可以只对需要同步的使用 b.与wait(),notify()和notifyall()方法使用比较方便 2.wait() a.释放持有的对象 ...
- Spring深入理解(二)
这个方法实现了 AbstractApplicationContext 的抽象方法 refreshBeanFactory,这段代码清楚的说明了 BeanFactory 的创建过程.注意 BeanFact ...
- dubbo协议参考手册(转)
原文链接:http://wely.iteye.com/blog/2331332 协议参考手册 (+) (#) 推荐使用Dubbo协议 性能测试报告各协议的性能情况,请参见:性能测试报告 (+) dub ...
- oracle regexp_like介绍和例子
oracle regexp_like介绍和例子 学习了:http://www.cnblogs.com/einyboy/archive/2012/08/01/2617606.html ORACLE中的支 ...
- 使用Html5和Js进行拖动
function init() { var source = document.getElementById("dragme"); ...
- Exchanger源代码剖析
Exchanger是一个针对线程可以结对交换元素的同步器.每条线程把某个对象作为參数调用exchange方法,与伙伴线程进行匹配.然后再函数返回的时接收伙伴的对象.另外.Exchanger内部实现採用 ...
- Python面向切面编程-语法层面和functools模块
1,Python语法层面对面向切面编程的支持(方法名装饰后改变为log) __author__ = 'Administrator' import time def log(func): def wra ...
- 黑马程序猿——JAVA基础——集合
----------android培训.java培训.java学习型技术博客.期待与您交流.------------ 一.关于java中的集合类 首先看一下,大致的框架流程图 ...