python open函數(shù)newline用法

寫這些文章主要是督促自己學(xué)習(xí),過去兩年時(shí)間,斷斷續(xù)續(xù)在學(xué)習(xí)linux、python以及機(jī)器學(xué)習(xí)方面的知識(shí),東西看了很多,卻沒有實(shí)際動(dòng)手操作,很多不明白的知識(shí)當(dāng)時(shí)解決后沒有記錄,現(xiàn)在也忘得差不多了。為此,希望能在這里記錄自己每天的學(xué)習(xí)軌跡,以后溫習(xí)時(shí)也方便。

1.不同操作系統(tǒng)換行符不統(tǒng)一

linux:\n? ? windows:\r\n? ? mac:\r

2.open函數(shù)newline用法

If?csvfile?is a file object, it should be opened with?newline=''.?


上述引用來自python?中關(guān)于csv標(biāo)準(zhǔn)庫的介紹,對(duì)于這句話相當(dāng)疑惑,因此,編寫以下程序來辨別。

Case 1: The file is read and written with newline=''.

csvfile=open('csvfile.csv','w',newline='')

writer=csv.writer(csvfile)

writer.writerow('a')

writer.writerow('b')

csvfile.close()

csvfile=open('csvfile.csv','r',newline='')

txtdata=csvfile.read()

csvfile.close()


最終,txtdata中的內(nèi)容為'a\r\nb\r\n'。

Case 2: The file is written with newline='', but read without it.?

csvfile=open('csvfile.csv','w',newline='')

writer=csv.writer(csvfile)

writer.writerow('a')

writer.writerow('b')

csvfile.close()

csvfile=open('csvfile.csv','r')

txtdata=csvfile.read()

csvfile.close()


最終,txtdata中的內(nèi)容為'a\r\nb\r\n'。

Case 3: The file is written without newline='', but read with it.?

csvfile=open('csvfile.csv','w')

writer=csv.writer(csvfile)

writer.writerow('a')

writer.writerow('b')

csvfile.close()

csvfile=open('csvfile.csv','r',newline='')

txtdata=csvfile.read()

csvfile.close()


最終,txtdata中的內(nèi)容為'a\r\r\nb\r\r\n'。

Case 4: The file is read and written without newline=''.

csvfile=open('csvfile.csv','w')

writer=csv.writer(csvfile)

writer.writerow('a')

writer.writerow('b')

csvfile.close()

csvfile=open('csvfile.csv','r')

txtdata=csvfile.read()

csvfile.close()


最終,txtdata中的內(nèi)容為'a\n\nb\n\n'。

原因分析

On input,if?newline?is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller. If it is '', universal newline mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated.

On output,if?newline?is None, any '\n' characters written are translated to the system default line separator,os.linesep. If?newline?is '', no translation takes place. If?new line?is any of the other legal values, any '\n' characters written are translated to the given string.


csv標(biāo)準(zhǔn)庫中的writerow在寫入文件時(shí)會(huì)加入'\r\n'作為換行符,if newline is '',換行符不會(huì)被轉(zhuǎn)化而是直接輸出,如case 1所示。

當(dāng)寫文件時(shí)newline='',程序?qū)懭?a\r\nb\r\n';讀取文件時(shí)newline=None,universal newlines mode工作,換行符'\r\n'被翻譯為'\n',如case 2所示。

當(dāng)寫文件時(shí)newline=None,csv先是將'a\r\nb\r\n'寫入內(nèi)存,再寫入文件時(shí),universal newlines mode工作,換行符'\n'被翻譯為'\r\n',最終結(jié)果如case 3所示。

當(dāng)寫文件時(shí)newline=None,csv先是將'a\r\nb\r\n'寫入內(nèi)存,再寫入文件時(shí),universal newlines mode工作,換行符'\n'被翻譯為'\r\n';讀取文件時(shí)newline=None,universal newlines mode工作,換行符'\r'和'\r\n'被翻譯為'\n',顯示為'a\n\nb\n\n',如case 4所示。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容