Python读写文件之换行符

摘要:
win10的txt文件中也能识别为换行符了同理,假设Windows上有一个dos.txt文件,那么,它在文件中的换行标志是,现在拷贝到linux下,linux遇到文件中的,认为是换行,至于其他的,认为是正常的字符。如此一来,就被当成了文件的正常部分,当这个文件是可执行脚本时,就会报错。
系统的换行符和路径分隔符

os模块可以获取当前系统的换行符和路径分隔符

windows操作系统

>>> os.linesep

' '

>>> os.sep

'\'

linux操作系统

>>> import os

>>> os.linesep #换行符

' '

>>> os.sep #路径分隔符

'/'

open函数的newline参数

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

读取文件

  • newline = None(默认)

不指定newline,则默认开启Universal newline mode,所有, , or 被默认转换为 ;

  • newline = ''

不做任何转换操作,读取到什么就是什么

  • newline = 'the other legal values'

按照给定的换行符界定行

简单而言,读取文件时,newline参数默认,不管换行符是什么,都会被转换为

写入文件

  • newline = None(默认)

字符会被转换为各系统默认的换行符(os.linesep)

windows的换行符是 ,但是写入时, 也会转换,转换为

  • newline = '' 或者newline = ' '

不做任何操作

  • newline = 'the other legal values'

字符会被转换为给定的值

简单而言,使用字符串的rstrip()方法,去掉末尾的各种换行符

然后,加上 ,

写文件时,newline参数默认, 字符会被转换为各系统默认的换行符(os.linesep)

示例1:编辑软件换行写,python原样读取和转换读取

向test.txt中写入如下内容:

Python读写文件之换行符第1张

with open('test.txt','r',newline='') as f:
print(repr(f.read()))
with open('test.txt','r') as f:
print(repr(f.read()))

'line1 line2'

'line1 line2'

结果符合预期

写入时

向txt写入时,回车插入

读取时

newline='',不做转换,原样输出'line1 line2'

newline = None,转换 为

示例2:python转换写 ,python原样读取和转换读取

with open('test.txt','w') as f:
f.write('line1line2')
with open('test.txt','r',newline='') as f:
print(repr(f.read()))

with open('test.txt','r') as f:
print(repr(f.read()))

'line1 line2'

'line1 line2'

这个结果符合预期

写入时

newline = None, 字符会被转换为各系统默认的换行符,会将 转换为

读取时

newline='',不会转换 ,原样输出

newline = None,会将 转换为

示例3:python原样写 ,python原样读取和转换读取

with open('test.txt','w',newline='') as f:
f.write('line1line2')
with open('test.txt','r',newline='') as f:
print(repr(f.read()))
with open('test.txt','r') as f:
print(repr(f.read()))

'line1 line2'

'line1 line2'

结果符合预期

写入时

newline='',不会转换,原样写入'line1 line2'

读取时

newline='',不会转换,原样输出'line1 line2'

newline = None,会转换 为 ,但是没有 ,所以显示的 ,也没问题

去掉字符串首尾的空白字符

, , ,空格等

字符串的strip(),lstrip(),rstrip()

str.strip去掉字符串头和尾的空白字符

>>> help(str.strip)

Help on method_descriptor:

strip(...)

S.strip([chars]) -> str

Return a copy of the string S with leading and trailing whitespace removed.

If chars is given and not None, remove characters in chars instead.

str.lstrip 去掉字符串头的空白字符

>>> help(str.lstrip)

Help on method_descriptor:

lstrip(...)

S.lstrip([chars]) -> str

Return a copy of the string S with leading whitespace removed.

If chars is given and not None, remove characters in chars instead.

str.rstrip去掉字符串尾的空白字符

>>> help(str.rstrip)

Help on method_descriptor:

rstrip(...)

S.rstrip([chars]) -> str

Return a copy of the string S with trailing whitespace removed.

If chars is given and not None, remove characters in chars instead.

拓展:linux和windows文件之间的拷贝

假设有一个linux下的unix.txt文件, 那么, 它在文件中的换行标志是: , 现在把unix.txt拷贝到Windows上, Windows找不到unix.txt中的 , 所以,对于Windows而言, 压根就没有发现unix.txt有任何换行, 所以, 我们从Windows上看到的unix.txt文件显示在一行里面。win10的txt文件中 也能识别为换行符了

同理, 假设Windows上有一个dos.txt文件, 那么, 它在文件中的换行标志是 , 现在拷贝到linux下, linux遇到文件中的 , 认为是换行, 至于其他的, 认为是正常的字符。 如此一来, 就被当成了文件的正常部分,当这个文件是可执行脚本时,就会报错。

win7中只有 被识别为换行符

>>> with open('test.txt','w',newline='') as f:

f.write('line1 line2 line3 line4')

24

>>> with open('test.txt','r',newline='') as f:

f.read()

'line1 line2 line3 line4'

Python读写文件之换行符第2张

win10中, , , 都可以识别为换行

>>> b' '.hex()

'0d'

>>> b' '.hex()

'0a'

以上 十六进制是0d, 十六进制是0a

示例1:

with open('test.txt','w',newline='') as f:
f.write('line1line2')
with open('test.txt','r',newline='') as f:
print(repr(f.read()))
with open('test.txt','r') as f:
print(repr(f.read()))

'line1 line2'

'line1 line2'

能换行

Python读写文件之换行符第3张

Python读写文件之换行符第4张

示例2:

with open('test.txt','w',newline='') as f:
f.write('line1line2')
with open('test.txt','r',newline='') as f:
print(repr(f.read()))
with open('test.txt','r') as f:
print(repr(f.read()))

'line1 line2'

'line1 line2'

能换行

Python读写文件之换行符第5张

Python读写文件之换行符第6张

示例3:

with open('test.txt','w',newline='') as f:
f.write('line1line2')
with open('test.txt','r',newline='') as f:
print(repr(f.read()))
with open('test.txt','r') as f:
print(repr(f.read()))

'line1 line2'

'line1 line2'

Python读写文件之换行符第7张

Python读写文件之换行符第8张

示例4:

with open('test.txt','w',newline='') as f:
f.write('line1line2')
with open('test.txt','r',newline='') as f:
print(repr(f.read()))
with open('test.txt','r') as f:
print(repr(f.read()))

'line1line2'

'line1line2'

和 都被识别为换行符

Python读写文件之换行符第9张

Python读写文件之换行符第10张

示例5: ,newline=None

字符会被转换为各系统默认的换行符(os.linesep)

这里没有

with open('test.txt','w') as f:
f.write('line1line2')
with open('test.txt','r',newline='') as f:
print(repr(f.read()))
with open('test.txt','r') as f:
print(repr(f.read()))

'line1 line2' #不做转换,原样读取

'line1 line2' # 转换为

示例6: ,newline=None

字符会被转换为各系统默认的换行符(os.linesep)

with open('test.txt','w') as f:
f.write('line1line2')
with open('test.txt','r',newline='') as f:
print(repr(f.read()))
with open('test.txt','r') as f:
print(repr(f.read()))

'line1 line2' #原样读取,不做转换,可以看到 在写入时转换为

'line1 line2' #转换读取, 转换为

示例7: ,newline=None

字符会被转换为各系统默认的换行符(os.linesep)

with open('test.txt','w') as f:
f.write('line1line2')
with open('test.txt','r',newline='') as f:
print(repr(f.read()))
with open('test.txt','r') as f:
print(repr(f.read()))

'line1 line2' #原样读取,不做转换, 并没有转换为 ,检测到了

'line1 line2' #转换读取, 转换为

python中,只有 被识别为换行符

Python读写文件之换行符第11张

word中, , , 都可以识别为换行

>>> print('line1 line2')

line1 line2

>>> print('line1 line2')

line1

line2

>>> print('line1 line2')

line1

line2

>>> print('line1 line2')

line1

line2

免责声明:文章转载自《Python读写文件之换行符》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇修复数据库索引问题:删除索引以提升性能wxWidgets文件操作(三)wxFileDialog与wxTextCtrl下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

python读写excel

Python操作excel表,需要用到两个库:xlrd和xlwt 1、获取excel表Book对象实例:   book = xlrd.open_workbook(filepath) 2、获取excel的Sheet对象   sheet_nums = book.nsheets    获取Sheet对象个数   sheet = book.sheet_by_ind...

python3.6升级及setuptools、pip安装

升级python3.6 1.打开官网www.python.org,找到最新3.6.3版本,复制下载链接 2.创建/app目录,wget下载到该目录下,编译安装 mkdir /app cd /app wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz tar xf Python-3...

在Ubuntu中创建一个简单的DJango项目

一:创建及运行虚拟环境 步骤说明 1. 使用命令行在Ubuntu系统下创建一个虚拟环境如创建一个叫py3的虚拟环境 irtualenv py3 2.进入创建好的虚拟环境 workon py3 3.在虚拟环境中安装Django, 代码说明 mkvirtualenv 创建虚拟环境 rmvirtualenv 删除虚拟环境 virtual 虚拟机...

使用 Python 和 Oracle 数据库实现高并发性

随着趋势发展的核心转向更多而不是更快发展,最大限度地提高并发性的重要性日益凸显。并发性使得编程模式发生了新的转变,可以编写异步代码,从而将多个任务分散到一组线程或进程中并行工作。如果您不是编程新手并且很熟悉 C 或 C++,您可能已经对线程和进程有所了解,并且知道它们之间的区别。在进行并发编程时,线程提供了进程的轻量级替代物,在大多数情况下多线程较多进程更...

ansible使用jinja2模板

jinja2基本语法 控制结构 {% %}             jinja2中的for循环用于迭代Python的数据类型,包括列表,元组和字典          2.变量取值 {{ }}             jinja2模板中使用 {{ }} 语法表示一个变量,它是一种特殊的占位符。当利用jinja2进行渲染的时候,它会把这些特殊的占位符进行填充/...

Python之地理信息可视化——matplot basemap工具箱

作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明。谢谢! 在数据可视化过程中,我们常常需要将数据根据其采集的地理位置在地图上显示出来。比如说我们会想要在地图上画出城市,飞机的航线,乃至于军事基地等等。通常来说,一个地理信息系统都会带有这样的功能。今天我们讨论如何在Python上实现,并且使用免费...