系统的换行符和路径分隔符
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中写入如下内容:
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('line1
line2')
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('line1
line2')
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'
win10中, , , 都可以识别为换行
>>> b' '.hex()
'0d'
>>> b' '.hex()
'0a'
以上 十六进制是0d, 十六进制是0a
示例1:
with open('test.txt','w',newline='') as f:
f.write('line1
line2')
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'
能换行
示例2:
with open('test.txt','w',newline='') as f:
f.write('line1
line2')
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'
能换行
示例3:
with open('test.txt','w',newline='') as f:
f.write('line1
line2')
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'
示例4:
with open('test.txt','w',newline='') as f:
f.write('line1
line2')
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'
和 都被识别为换行符
示例5: ,newline=None
字符会被转换为各系统默认的换行符(os.linesep)
这里没有
with open('test.txt','w') as f:
f.write('line1
line2')
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('line1
line2')
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('line1
line2')
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中,只有 被识别为换行符
word中, , , 都可以识别为换行
>>> print('line1 line2')
line1 line2
>>> print('line1 line2')
line1
line2
>>> print('line1 line2')
line1
line2
>>> print('line1 line2')
line1
line2