系統的換行符和路徑分隔符
os模塊可以獲取當前系統的換行符和路徑分隔符
windows操作系統
>>> os.linesep
'\r\n'
>>> os.sep
'\\'
linux操作系統
>>> import os
>>> os.linesep #換行符
'\n'
>>> 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,所有\n, \r, or \r\n被默認轉換為\n ;
-
newline = ''
不做任何轉換操作,讀取到什么就是什么
-
newline = 'the other legal values'
按照給定的換行符界定行
簡單而言,讀取文件時,newline參數默認,不管換行符是什么,都會被轉換為\n
寫入文件
-
newline = None(默認)
\n字符會被轉換為各系統默認的換行符(os.linesep)
windows的換行符是\r\n,但是寫入時,\r\n也會轉換,轉換為\r\r\n
-
newline = '' 或者newline = '\n'
不做任何操作
-
newline = 'the other legal values'
\n字符會被轉換為給定的值
簡單而言,使用字符串的rstrip()方法,去掉末尾的各種換行符
然后,加上\n,
寫文件時,newline參數默認,\n字符會被轉換為各系統默認的換行符(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\r\nline2'
'line1\nline2'
結果符合預期
寫入時
向txt寫入時,回車插入\r\n
讀取時
newline='',不做轉換,原樣輸出'line1\r\nline2'
newline = None,轉換\r\n為\n
示例2:python轉換寫\n,python原樣讀取和轉換讀取
with open('test.txt','w') as f:
f.write('line1\nline2')
with open('test.txt','r',newline='') as f:
print(repr(f.read()))
with open('test.txt','r') as f:
print(repr(f.read()))
'line1\r\nline2'
'line1\nline2'
這個結果符合預期
寫入時
newline = None,\n字符會被轉換為各系統默認的換行符,會將\n轉換為\r\n
讀取時
newline='',不會轉換\r\n,原樣輸出
newline = None,會將\r\n轉換為\n
示例3:python原樣寫\n,python原樣讀取和轉換讀取
with open('test.txt','w',newline='') as f:
f.write('line1\nline2')
with open('test.txt','r',newline='') as f:
print(repr(f.read()))
with open('test.txt','r') as f:
print(repr(f.read()))
'line1\nline2'
'line1\nline2'
結果符合預期
寫入時
newline='',不會轉換,原樣寫入'line1\nline2'
讀取時
newline='',不會轉換,原樣輸出'line1\nline2'
newline = None,會轉換\r\n為\n,但是沒有\r\n,所以顯示的\n,也沒問題
去掉字符串首尾的空白字符
\n,\r,\t,空格等
字符串的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文件, 那么, 它在文件中的換行標志是:\n, 現在把unix.txt拷貝到Windows上, Windows找不到unix.txt中的\r\n, 所以,對於Windows而言, 壓根就沒有發現unix.txt有任何換行, 所以, 我們從Windows上看到的unix.txt文件顯示在一行里面。win10的txt文件中\n也能識別為換行符了
同理, 假設Windows上有一個dos.txt文件, 那么, 它在文件中的換行標志是\r\n, 現在拷貝到linux下, linux遇到文件中的\n, 認為是換行, 至於其他的, 認為是正常的字符。 如此一來, \r就被當成了文件的正常部分,當這個文件是可執行腳本時,就會報錯。
win7中只有\r\n被識別為換行符
>>> with open('test.txt','w',newline='') as f:
f.write('line1\rline2\nline3\r\nline4')
24
>>> with open('test.txt','r',newline='') as f:
f.read()
'line1\rline2\nline3\r\nline4'
win10中,\r,\n,\r\n都可以識別為換行
>>> b'\r'.hex()
'0d'
>>> b'\n'.hex()
'0a'
以上\r十六進制是0d,\n十六進制是0a
示例1:\r
with open('test.txt','w',newline='') as f:
f.write('line1\rline2')
with open('test.txt','r',newline='') as f:
print(repr(f.read()))
with open('test.txt','r') as f:
print(repr(f.read()))
'line1\rline2'
'line1\nline2'
\r能換行
示例2:\n
with open('test.txt','w',newline='') as f:
f.write('line1\nline2')
with open('test.txt','r',newline='') as f:
print(repr(f.read()))
with open('test.txt','r') as f:
print(repr(f.read()))
'line1\nline2'
'line1\nline2'
\n能換行
示例3:\r\n
with open('test.txt','w',newline='') as f:
f.write('line1\r\nline2')
with open('test.txt','r',newline='') as f:
print(repr(f.read()))
with open('test.txt','r') as f:
print(repr(f.read()))
'line1\r\nline2'
'line1\nline2'
示例4:\r\r\n
with open('test.txt','w',newline='') as f:
f.write('line1\r\r\nline2')
with open('test.txt','r',newline='') as f:
print(repr(f.read()))
with open('test.txt','r') as f:
print(repr(f.read()))
'line1\r\r\nline2'
'line1\n\nline2'
\r和\r\n都被識別為換行符
示例5:\r,newline=None
\n字符會被轉換為各系統默認的換行符(os.linesep)
這里沒有\n
with open('test.txt','w') as f:
f.write('line1\rline2')
with open('test.txt','r',newline='') as f:
print(repr(f.read()))
with open('test.txt','r') as f:
print(repr(f.read()))
'line1\rline2' #不做轉換,原樣讀取
'line1\nline2' #\r轉換為\n
示例6:\n,newline=None
\n字符會被轉換為各系統默認的換行符(os.linesep)
with open('test.txt','w') as f:
f.write('line1\nline2')
with open('test.txt','r',newline='') as f:
print(repr(f.read()))
with open('test.txt','r') as f:
print(repr(f.read()))
'line1\r\nline2' #原樣讀取,不做轉換,可以看到\n在寫入時轉換為\r\n
'line1\nline2' #轉換讀取,\r\n轉換為\n
示例7:\r\n,newline=None
\n字符會被轉換為各系統默認的換行符(os.linesep)
with open('test.txt','w') as f:
f.write('line1\r\nline2')
with open('test.txt','r',newline='') as f:
print(repr(f.read()))
with open('test.txt','r') as f:
print(repr(f.read()))
'line1\r\r\nline2' #原樣讀取,不做轉換,\r\r\n並沒有轉換為\r\r\r\n,檢測到了\r\n
'line1\n\nline2' #轉換讀取,\r\n轉換為\n
python中,只有\n被識別為換行符
word中,\r,\n,\r\n都可以識別為換行
>>> print('line1\rline2')
line1 line2
>>> print('line1\nline2')
line1
line2
>>> print('line1\r\nline2')
line1
line2
>>> print('line1\r\r\nline2')
line1
line2