问题:在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:文件读取——换行符的问题的更多相关文章

  1. python处理文件的换行符

    我们知道在Windows平台下的换行符是\r\n,而在linux下的换行符是\n.现在写一个简单程序来测试python是如何处理这些换行符的. 准备文件data.txt,该文件在Windows平台下编 ...

  2. Python编码/文件读取/多线程

    Python编码/文件读取/多线程 个人笔记~~记录才有成长   编码/文件读取/多线程 编码 常用的一般是gbk.utf-8,而在python中字符串一般是用Unicode来操作,这样才能按照单个字 ...

  3. python大文件读取

    python大文件读取 https://stackoverflow.com/questions/8009882/how-to-read-a-large-file-line-by-line-in-pyt ...

  4. python写文件无法换行的问题

    python写文件无法换行的问题,用'\n'  不行,直接打印的出来了. 网上查了查,都说是用  ‘\r\n’ ,但是这样打出来,不仅换行了,还加了一个空行. windows平台最后结果是    直接 ...

  5. C语言中以文本方式读写文件时换行符转换的注意事项

    我们知道在UNIX下是没有回车符(\r)的,只有换行符(\n),而C语言诞生于UNIX(Linux即面向开源的UNIX,Mac OS也是UNIX发展而来的,而Windows是从MS-DOS发展而来,与 ...

  6. Linux文件和windows文件在 换行符的区别

    Linux或Unix文件,和windows文件,在来回处理时,如果不注意 换行符的区别,可能导致程序错误!!!深刻的教训.... 在早期的打印机时代,开始新的一行要占用两个字符的时间.如果到了一行的结 ...

  7. ****使用ftp软件上传下载php文件时换行符丢失bug

    在使用ftp软件上传下载php源文件时,我们偶尔会发现在本地windows下notepad++编辑器写好的php文件,在使用ftp上传到linux服务器后,php文件的换行符全部丢失了,导致php文件 ...

  8. 记一次错误排查,主要问题是跨平台文件中换行符(CRLF, LF)和垃圾字符( Caret Notation)

    笔者测试SPEC14的workload的时候,需要自定义workload,又需要在Windows和Linux平台上都要测试,所以就遇到了这么个问题:测试工具报错,但是报出来的错误信息又跟错误对不上. ...

  9. 【Python】使用制表符换行符来添加空白

    在编程中,在打印时,有时候需要显示出来的数据看着舒服一点,那么使用制表符(\t).换行符(\n)即可轻松实现 >>> print('zhangsan')zhangsan 加入制表符后 ...

随机推荐

  1. Linux系统学习之 三:新手必须掌握的Linux命令3

    内容预览 1.输入输出重定向 2.管道命令符 3.命令行的通配符 4.常用的转义符号 5.重要的环境变量 一.输入输出重定向 重定向技术的5种模式:1 标准覆盖输出重定向 错误覆盖输出重定向 错误追加 ...

  2. 游标后面select 带有in时

    今天遇到一个问题,使用游标时,在给游标填充值的时候,select  语句中带有 where查询条件,并且还有 in子句. 本来我是这样写的,试了很多次都不出结果,当然number in (304010 ...

  3. 【ACM】hdu_2115_I Love This Game_201308021517

    I Love This GameTime Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  4. IntelliJ IDEA测试学习网站

    IntelliJ IDEA测试学习网站 http://idea.lanyus.com/  嗯,请支持正版:

  5. 18110 Koishi's travel, Satori's travel

    18110 Koishi's travel, Satori's travel 该题有题解 时间限制:4000MS  内存限制:65535K提交次数:0 通过次数:0 题型: 编程题   语言: 不限定 ...

  6. pyhton 从web获取json数据 保存到本地然后再读取

    从web中获取json数据直接进行处理总认为太慢.主要是从web中获取获取数据的过程有点慢. 所以就在想 假设先利用空暇时间把json数据获取并保存到本地,然后再从本地文件里读取和操作.应该就要快非常 ...

  7. 关于ShapeDrawable应用的一些介绍(中)之Gradient

    版权声明:本文为博主原创文章,未经博主允许不得转载. Gradient,渐变,是在界面设计中最经常用到的一种技巧,只要涉及到颜色的处理,浓妆淡抹总相宜,说的就是它. 在Android中,当然也提供了这 ...

  8. Configure environment variables for different tools in jenkins

    安装以下的工具,并在Jenkins中的Manage Jenkins-->Configure System-->Global Properties-->Environment Vari ...

  9. 扩展函数之 IsWhat 简单好用

    代码实现: /***扩展函数名细***/ //[IsInRange] ; //以前写法 & num < ) { } //现在写法 , )) { } //datetime类型也支持 //[ ...

  10. C语言 - .c和.h文件的困惑

    本质上没有任何区别. 只不过一般:.h文件是头文件,内含函数声明.宏定义.结构体定义等内容. .c文件是程序文件,内含函数实现,变量定义等内容.而且是什么后缀也没有关系,只不过编译器会默认对某些后缀的 ...