這篇文章給大家分享的是有關(guān)Python3中io文本及原始流I/O工具怎么用的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
創(chuàng)新互聯(lián)是專業(yè)的泗縣網(wǎng)站建設(shè)公司,泗縣接單;提供網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行泗縣網(wǎng)站開發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!io模塊在解釋器的內(nèi)置open()之上實(shí)現(xiàn)了一些類來完成基于文件的輸入和輸出操作。這些類得到了適當(dāng)?shù)姆纸?,從而可以針?duì)不同的用途重新組合——例如,支持向一個(gè)網(wǎng)絡(luò)套接字寫Unicode數(shù)據(jù)。
1.1 內(nèi)存中的流
StringIO提供了一種很便利的方式,可以使用文件API(如read()、write()等)處理內(nèi)存中的文本。有些情況下,與其他一些字符串連接技術(shù)相比,使用StringIO構(gòu)造大字符串可以提供更好的性能。內(nèi)存中的流緩沖區(qū)對(duì)測(cè)試也很有用,寫入磁盤上真正的文件并不會(huì)減慢測(cè)試套件的速度。
下面是使用StringIO緩沖區(qū)的一些標(biāo)準(zhǔn)例子。
import io # Writing to a buffer output = io.StringIO() output.write('This goes into the buffer. ') print('And so does this.', file=output) # Retrieve the value written print(output.getvalue()) output.close() # discard buffer memory # Initialize a read buffer input = io.StringIO('Inital value for read buffer') # Read from the buffer print(input.read())
這個(gè)例子使用了read(),不過也可以用readline()和readlines()方法。StringIO類還提供了一個(gè)seek()方法,讀取文本時(shí)可以在緩沖區(qū)中跳轉(zhuǎn),如果使用一種前向解析算法,則這個(gè)方法對(duì)于回轉(zhuǎn)很有用。
要處理原始字節(jié)而不是Unicode文本,可以使用BytesIO。
import io # Writing to a buffer output = io.BytesIO() output.write('This goes into the buffer. '.encode('utf-8')) output.write('ÁÇÊ'.encode('utf-8')) # Retrieve the value written print(output.getvalue()) output.close() # discard buffer memory # Initialize a read buffer input = io.BytesIO(b'Inital value for read buffer') # Read from the buffer print(input.read())
寫入BytesIO實(shí)例的值一定是bytes而不是str。
1.2 為文本數(shù)據(jù)包裝字節(jié)流
原始字節(jié)流(如套接字)可以被包裝為一個(gè)層來處理串編碼和解碼,從而可以更容易地用于處理文本數(shù)據(jù)。TextIOWrapper類支持讀寫。write_through參數(shù)會(huì)禁用緩沖,并且立即將寫至包裝器的所有數(shù)據(jù)刷新輸出到底層緩沖區(qū)。
import io # Writing to a buffer output = io.BytesIO() wrapper = io.TextIOWrapper( output, encoding='utf-8', write_through=True, ) wrapper.write('This goes into the buffer. ') wrapper.write('ÁÇÊ') # Retrieve the value written print(output.getvalue()) output.close() # discard buffer memory # Initialize a read buffer input = io.BytesIO( b'Inital value for read buffer with unicode characters ' + 'ÁÇÊ'.encode('utf-8') ) wrapper = io.TextIOWrapper(input, encoding='utf-8') # Read from the buffer print(wrapper.read())
這個(gè)例子使用了一個(gè)BytesIO實(shí)例作為流。對(duì)應(yīng)bz2、http,server和subprocess的例子展示了如何對(duì)其他類型的類似文件的對(duì)象使用TextIOWrapper。
感謝各位的閱讀!關(guān)于“Python3中io文本及原始流I/O工具怎么用”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
本文題目:Python3中io文本及原始流I/O工具怎么用-創(chuàng)新互聯(lián)
標(biāo)題URL:http://aaarwkj.com/article32/cdhgsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、外貿(mào)建站、定制開發(fā)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容