日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

python?open函數(shù)中newline參數(shù)實(shí)例詳解_python

作者:我是個(gè)燒餅啊 ? 更新時(shí)間: 2022-08-14 編程語言

問題的由來

我在讀pythoncsv模塊文檔 看到了這樣一句話

如果 csvfile 是文件對(duì)象,則打開它時(shí)應(yīng)使用 newline=‘’。
其備注:如果沒有指定 newline=‘’,則嵌入引號(hào)中的換行符將無法正確解析,并且在寫入時(shí),使用 \r\n 換行的平臺(tái)會(huì)有多余的 \r 寫入。由于 csv 模塊會(huì)執(zhí)行自己的(通用)換行符處理,因此指定 newline=‘’ 應(yīng)該總是安全的。

我就在思考o(jì)pen函數(shù)中的newline參數(shù)的作用,因?yàn)樽约褐霸谑褂胦pen函數(shù)時(shí)從來沒有設(shè)置過newline參數(shù),僅從上面官方給的備注理解newline參數(shù)可以幫助處理換行符解析的問題

并且查閱得知不同操作系統(tǒng)換行符并不一致:

Unix 的行結(jié)束約定 ‘\n’、Windows 的約定 ‘\r\n’ 以及舊版 Macintosh 的約定 ‘\r’

打破了我原本觀念以為的換行符就是\n

python官方文檔對(duì)newline參數(shù)解釋:

newline 控制 universal newlines 模式如何生效(它僅適用于文本模式)。它可以是 None,‘’,‘\n’,‘\r’ 和 ‘\r\n’。它的工作原理:
從流中讀取輸入時(shí),如果 newline 為 None,則啟用通用換行模式。輸入中的行可以以 ‘\n’,‘\r’ 或 ‘\r\n’ 結(jié)尾,這些行被翻譯成 ‘\n’ 在返回呼叫者之前。如果它是 ‘’,則啟用通用換行模式,但行結(jié)尾將返回給調(diào)用者未翻譯。如果它具有任何其他合法值,則輸入行僅由給定字符串終止,并且行結(jié)尾將返回給未調(diào)用的調(diào)用者。
將輸出寫入流時(shí),如果 newline 為 None,則寫入的任何 ‘\n’ 字符都將轉(zhuǎn)換為系統(tǒng)默認(rèn)行分隔符 os.linesep。如果 newline 是 ‘’ 或 ‘\n’,則不進(jìn)行翻譯。如果 newline 是任何其他合法值,則寫入的任何 ‘\n’ 字符將被轉(zhuǎn)換為給定的字符串。

從這也就理解了為什么原本使用open()寫的時(shí)候用\n就可以表示換行以及讀文本文件時(shí)行尾會(huì)返回\n

  • 寫入的時(shí)候沒有指定newline參數(shù)會(huì)將\n翻譯成系統(tǒng)默認(rèn)的行分割符(\r\n)
  • 讀的時(shí)候沒有指定newline參數(shù)會(huì)將行分割符(\r\n)翻譯為\n

回到上文,那為什么在讀寫csv文件時(shí)就要設(shè)置newline=''呢?

pythoncsv官方文檔解釋了這一問題(這也就引入了第二種方法解決換行的問題,我在后面會(huì)介紹到)

Dialect.lineterminator
放在 writer 產(chǎn)生的行的結(jié)尾,默認(rèn)為 ‘\r\n’。
注解 reader 經(jīng)過硬編碼,會(huì)識(shí)別 ‘\r’ 或 ‘\n’ 作為行尾,并忽略 lineterminator。未來可能會(huì)更改這一行為。

用白話說就是writerow()方法在寫入一行數(shù)據(jù)時(shí)在行尾都會(huì)跟一個(gè)默認(rèn)換行符(\r\n)(即csv是將’一行數(shù)據(jù)\r\n’寫入內(nèi)存,此時(shí)這一行數(shù)據(jù)還在內(nèi)存中,還沒有寫入文件)之后執(zhí)行代碼真正在向文件寫入時(shí)根據(jù)不同newline參數(shù)進(jìn)行翻譯
而在向txt文件使用write()方法寫入內(nèi)容時(shí)是我們手動(dòng)添加換行符\n(內(nèi)存中的數(shù)據(jù)就是我們寫入的內(nèi)容,并不會(huì)隱式添加其他內(nèi)容)之后執(zhí)行代碼真正在向文件寫入時(shí)根據(jù)newline參數(shù)進(jìn)行翻譯,這就是二者的區(qū)別
具體流程:
newline=‘’
writer.writerow(‘line’) 實(shí)際是向內(nèi)存中寫入’line\r\n’ --》 執(zhí)行代碼,寫入文件,根據(jù)newline=‘’,將不進(jìn)行翻譯 --》文件最終寫入’line\r\n’
newline=None(默認(rèn))
f.write(‘line\n’) 直接將’line\n’寫入內(nèi)存 --》 執(zhí)行代碼,寫入文件,根據(jù)newline=None,將\n翻譯為\r\n --》文件最終寫入’line\r\n’

具體實(shí)例

case1: w newline=‘’ r newline=‘’

import csv
with open("test.csv","w",encoding='utf-8',newline='') as csvfile:
	    writer=csv.writer(csvfile)
	    writer.writerow(["num","name","grade"])
	    writer.writerows([[1,'luke','96'],[2,'jack','85'],[3,'nick','84']])
with open("test.csv","r",encoding='utf-8',newline='') as csvfile:
	    txtdata=csvfile.read()
txtdata      #>>'num,name,grade\r\n1,luke,96\r\n2,jack,85\r\n3,nick,84\r\n'

case2: w newline=‘\r’ r newline=‘’

import csv
with open("test.csv","w",encoding='utf-8',newline='\r') as csvfile:
	    writer=csv.writer(csvfile)
	    writer.writerow(["num","name","grade"])
	    writer.writerows([[1,'luke','96'],[2,'jack','85'],[3,'nick','84']])
with open("test.csv","r",encoding='utf-8',newline='') as csvfile:
	    txtdata=csvfile.read()
txtdata  #>>'num,name,grade\r\r1,luke,96\r\r2,jack,85\r\r3,nick,84\r\r'

case3: w newline=‘\r\n’ r newline=‘’

import csv
with open("test.csv","w",encoding='utf-8',newline='\r\n') as csvfile:
	    writer=csv.writer(csvfile)
	    writer.writerow(["num","name","grade"])
	    writer.writerows([[1,'luke','96'],[2,'jack','85'],[3,'nick','84']])
with open("test.csv","r",encoding='utf-8',newline='') as csvfile:
	    txtdata=csvfile.read()
txtdata  #>>'num,name,grade\r\r\n1,luke,96\r\r\n2,jack,85\r\r\n3,nick,84\r\r\n'

case4: w newline=None r newline=None

import csv
with open("test.csv","w",encoding='utf-8',newline=None) as csvfile:
	    writer=csv.writer(csvfile)
	    writer.writerow(["num","name","grade"])
	    writer.writerows([[1,'luke','96'],[2,'jack','85'],[3,'nick','84']])
with open("test.csv","r",encoding='utf-8',newline=None) as csvfile:
	    txtdata=csvfile.read()
txtdata  #>>'num,name,grade\n\n1,luke,96\n\n2,jack,85\n\n3,nick,84\n\n'

case5: 文件寫入為\r\r\n 文件讀取 newline=‘\r’

with open("test.csv","r",encoding='utf-8',newline='') as csvfile:
	txtdata=csvfile.read()
txtdata  #>>'num,name,grade\r\r\n1,luke,96\r\r\n2,jack,85\r\r\n3,nick,84\r\r\n'
import csv
with open("test.csv","r",encoding='utf-8',newline='\r') as csvfile:
    content = csv.reader(csvfile)
    for i in content:
        print(i)

為什么會(huì)報(bào)錯(cuò):

csv.reader是如何讀取\r\r\n的:讀取時(shí)遇到\r認(rèn)為一行結(jié)束了,再一次遇到\r同樣認(rèn)為一行結(jié)束(因而返回了空串列表),遇到\n無法解釋–》報(bào)錯(cuò)

case6:文件寫入為\r\r\n 文件讀取 newline=‘\n’

with open("test.csv","r",encoding='utf-8',newline='') as csvfile:
    txtdata=csvfile.read()
txtdata  #>>'num,name,grade\r\r\n1,luke,96\r\r\n2,jack,85\r\r\n3,nick,84\r\r\n'
import csv
with open("test.csv","r",encoding='utf-8',newline='\n') as csvfile:
    content = csv.reader(csvfile)
    for i in content:
        print(i)

case7:文件寫入為\r\r\n 文件讀取newline=‘\r\n’

with open("test.csv","r",encoding='utf-8',newline='') as csvfile:
    txtdata=csvfile.read()
txtdata  #>>'num,name,grade\r\r\n1,luke,96\r\r\n2,jack,85\r\r\n3,nick,84\r\r\n'
import csv
with open("test.csv","r",encoding='utf-8',newline='\r\n') as csvfile:
    content = csv.reader(csvfile)
    for i in content:
        print(i)

case8:文件寫入為\r\r 文件讀取 newline=‘\r’

with open("test.csv","r",encoding='utf-8',newline='') as csvfile:
    txtdata=csvfile.read()
txtdata  #>>'num,name,grade\r\r1,luke,96\r\r2,jack,85\r\r3,nick,84\r\r'
import csv
with open("test.csv","r",encoding='utf-8',newline='\r') as csvfile:
    content = csv.reader(csvfile)
    for i in content:
        print(i)

第二種方法:通過設(shè)置csv.writer方法中的lineterminator參數(shù)

上面提到lineterminator參數(shù)控制writer寫入每一行后跟的隱式結(jié)束符,默認(rèn)為’\r\n’,因此我們需要要設(shè)置lineterminator=‘\n’,讀取時(shí)也不需要設(shè)置newline參數(shù)即可獲得想要的效果

import csv
with open("test.csv","w",encoding='utf-8') as csvfile:
    writer=csv.writer(csvfile,lineterminator='\n')
    writer.writerow(["num","name","grade"])
    writer.writerows([[1,'luke','96'],[2,'jack','85'],[3,'nick','84']])
with open("test.csv","r",encoding='utf-8') as csvfile:
    lst=csv.reader(csvfile)
    csvfile.seek(0)
    txtdata = csvfile.read()
    csvfile.seek(0)
    for i in lst:
        print(i)
txtdata #>>'num,name,grade\n1,luke,96\n2,jack,85\n3,nick,84\n'

總結(jié)

原文鏈接:https://blog.csdn.net/Reborn214/article/details/123974467

欄目分類
最近更新