讀取、寫入和 Python
編寫程序的最后一個基本步驟就是從文件讀取數(shù)據(jù)和把數(shù)據(jù)寫入文件。閱讀完這篇文章之后,可以在自己的 to-do 列表中加上檢驗(yàn)這個技能學(xué)習(xí)效果的任務(wù)。
簡單輸出
貫穿整個系列,一直用 print 語句寫入(輸出)數(shù)據(jù),它默認(rèn)把表達(dá)式作為 string 寫到屏幕上(或控制臺窗口上)。清單 1 演示了這一點(diǎn)。清單 1 重復(fù)了第一個 Python 程序 “Hello, World!”,但是做了一些小的調(diào)整。
清單 1. 簡單輸出
>>> print "Hello World!" Hello World! >>> print "The total value is = $", 40.0*45.50 The total value is = $ 1820.0 >>> print "The total value = $%6.2f" % (40.0*45.50) The total value = $1820.00 >>> myfile = file("testit.txt", 'w') >>> print >> myfile, "Hello World!" >>> print >> myfile, "The total value = $%6.2f" % (40.0*45.50) >>> myfile.close()
正如這個示例演示的,用 print 語句寫入數(shù)據(jù)很容易。首先,示例輸出一個簡單的 string。然后創(chuàng)建并輸出復(fù)合的 string,這個字符串是用 string 格式化技術(shù)創(chuàng)建的。
但是,在這之后,事情發(fā)生了變化,與代碼以前的版本不同。接下來的一行創(chuàng)建 file 對象,傳遞進(jìn)名稱 "testit.txt" 和 'w' 字符(寫入文件)。然后使用修改過的 print 語句 ―― 兩個大于號后邊跟著容納 file 對象的變量 ―― 寫入相同的 string。但是這一次,數(shù)據(jù)不是在屏幕上顯示。很自然的問題是:數(shù)據(jù)去哪兒了?而且,這個 file 對象是什么?
第一個問題很容易回答。請查找 testit.txt 文件,并像下面那樣顯示它的內(nèi)容。
% more testit.txt Hello World! The total value = $1820.00
可以看到,數(shù)據(jù)被準(zhǔn)確地寫入文件,就像以前寫到屏幕上一樣。
現(xiàn)在,請注意清單 1 中的最后一行,它調(diào)用 file 對象的 close 方法。在 Python 程序中這很重要,因?yàn)樵谀J(rèn)情況下,文件輸入和輸出是緩沖的;在調(diào)用 print 語句時(shí),數(shù)據(jù)實(shí)際未被寫入;相反,數(shù)據(jù)是成批寫入的。讓 Python 把數(shù)據(jù)寫入文件的最簡單方式就是顯式地調(diào)用 close 方法。
文件對象
file 是與計(jì)算機(jī)上的文件進(jìn)行交互的基本機(jī)制。可以用 file 對象讀取數(shù)據(jù)、寫入數(shù)據(jù)或把數(shù)據(jù)添加到文件,以及處理二進(jìn)制或文本數(shù)據(jù)。
學(xué)習(xí) file 對象的最簡單方法就是閱讀幫助,如清單 2 所示。
清單 2. 得到 file 對象的幫助
>>> help(file) Help on class file in module __builtin__: class file(object) | file(name[, mode[, buffering]]) -> file object | | Open a file. The mode can be 'r', 'w' or 'a' for reading (default), | writing or appending. The file will be created if it doesn't exist | when opened for writing or appending; it will be truncated when | opened for writing. Add a 'b' to the mode for binary files. | Add a '+' to the mode to allow simultaneous reading and writing. | If the buffering argument is given, 0 means unbuffered, 1 means line | buffered, and larger numbers specify the buffer size. | Add a 'U' to mode to open the file for input with universal newline | support. Any line ending in the input file will be seen as a '\n' | in Python. Also, a file so opened gains the attribute 'newlines'; | the value for this attribute is one of None (no newline read yet), | '\r', '\n', '\r\n' or a tuple containing all the newline types seen. | | 'U' cannot be combined with 'w' or '+' mode. | | Note: open() is an alias for file(). | | Methods defined here: ...
正如幫助工具指出的,使用 file 對象很簡單。用 file 構(gòu)造函數(shù)或 open 方法創(chuàng)建 file 對象,open 是 file 構(gòu)造函數(shù)的別名。第二個參數(shù)是可選的,它指定文件的使用方式:
- ??? 'r' (默認(rèn)值)表示從文件讀取數(shù)據(jù)。
- ??? 'w' 表示要向文件寫入數(shù)據(jù),并截?cái)嘁郧暗膬?nèi)容。
- ??? 'a' 表示要向文件寫入數(shù)據(jù),但是添加到當(dāng)前內(nèi)容尾部。
- ??? 'r+' 表示對文件進(jìn)行讀寫操作(刪除以前的所有數(shù)據(jù))。
- ??? 'r+a' 表示對文件進(jìn)行讀寫操作(添加到當(dāng)前內(nèi)容尾部)。
- ??? 'b' 表示要讀寫二進(jìn)制數(shù)據(jù)。
這篇文章的第一個代碼清單向文件寫入數(shù)據(jù)。現(xiàn)在,清單 3 顯示如何把這個數(shù)據(jù)讀入 Python 程序,并解析文件的內(nèi)容。
清單 3. 從文件讀取數(shù)據(jù)
>>> myfile = open("testit.txt") >>> myfile.read() 'Hello World!\nThe total value = $1820.00\n' >>> str = myfile.read() >>> print str >>> myfile.seek(0) >>> str = myfile.read() >>> print str Hello World! The total value = $1820.00 >>> str.split() ['Hello', 'World!', 'The', 'total', 'value', '=', '$1820.00'] >>> str.split('\n') ['Hello World!', 'The total value = $1820.00', ''] >>> for line in str.split('\n'): ... print line ... Hello World! The total value = $1820.00 >>> myfile.close()
要讀取數(shù)據(jù),首先要創(chuàng)建合適的 file 對象 ―― 在這個示例中,文件對象打開 testit.txt 文件,并用 read 方法讀取內(nèi)容。這個方法把整個文件讀入一個 string,然后在程序中把這個字符串輸出到控制臺。在對 read 方法的第二個調(diào)用中,試圖把值分配給 str 變量,結(jié)果返回一個空的 string。這是因?yàn)榈谝粋€ read 操作讀入了整個文件。當(dāng)試圖再次讀取內(nèi)容時(shí),已經(jīng)到了文件末尾,所以什么也讀不到。
這個問題的解決方案也很簡單:讓 file 對象返回文件的開頭。回到開頭要通過 seek 方法進(jìn)行,它接受一個參數(shù),表示要從文件中的什么位置開始讀取或?qū)懭耄ɡ纾? 代表文件開頭)。seek 方法支持更復(fù)雜的操作,但是可能會有危險(xiǎn)。對于目前來說,我們還堅(jiān)持采用簡單方式。
現(xiàn)在回到了文件的開始之處,可以把文件內(nèi)容讀入 string 變量并對 string 做適當(dāng)?shù)亟馕觥U堊⒁猓谖募校兄g用新行(或行結(jié)束)字符區(qū)分。如果試著在 string 上調(diào)用 split 方法,它會在空白字符(例如空格)處進(jìn)行拆分。為了讓方法根據(jù)新行字符拆分各行,必須顯式地指定新行字符。然后可以拆分 string 并在 for 循環(huán)中對文件的行進(jìn)行迭代。
看起來僅僅從文件中讀取和處理一行內(nèi)容都有許多工作要做。Python 要讓簡單的事情變?nèi)菀祝阅赡芟胫肋@個任務(wù)有沒有快捷方式可用。如清單 4 所示,答案是 yes。
清單 4. 讀取和解析行
>>> myfile = open("testit.txt") >>> for line in myfile.readlines(): ... print line ... Hello World! The total value = $1820.00 >>> myfile.close() >>> for line in open("testit.txt").readlines(): ... print line ... Hello World! The total value = $1820.00 >>> for line in open("testit.txt"): ... print line ... Hello World! The total value = $1820.00
清單 4 演示了讀取和解析文本文件行的三種技術(shù)。首先,打開文件并把它分配給變量。然后調(diào)用 readlines 方法,把整個文件讀入內(nèi)存并把內(nèi)容拆分成 string 列表。for 循環(huán)在 string 列表上進(jìn)行迭代,一次輸出一行。
第二個 for 循環(huán)通過使用 file 對象的隱式變量(也就是說,變量不是顯式創(chuàng)建的),對這個過程稍做了點(diǎn)兒簡化。打開文件和讀取文件內(nèi)容一次完成,生成的效果與第一個顯式示例相同。最后一個示例進(jìn)一步做了簡化,并演示了直接在 file 對象上進(jìn)行迭代的能力(請注意,這是 Python 的一個新特性,所以在您的計(jì)算機(jī)上可能無法工作)。在這個示例中,創(chuàng)建隱式 file 對象,然后 Python 做余下的工作,允許對文件中的全部行進(jìn)行迭代。
但是,有些時(shí)候,在從文件讀取數(shù)據(jù)時(shí),可能想要更好的控制級別。在這種情況下,應(yīng)當(dāng)使用 readline 方法,如清單 5 所示。
清單 5. 讀取數(shù)據(jù)
>>> myfile = open("testit.txt") >>> myfile.readline() 'Hello World!\n' >>> myfile.readline() 'The total value = $1820.00\n' >>> myfile.readline() '' >>> myfile.seek(0) >>> myfile.readline() 'Hello World!\n' >>> myfile.tell() 13L >>> myfile.readline() 'The total value = $1820.00\n' >>> myfile.tell() 40L >>> myfile.readline() '' >>> myfile.tell() 40L >>> myfile.seek(0) >>> myfile.read(17) 'Hello World!\nThe ' >>> myfile.seek(0) >>> myfile.readlines(23) ['Hello World!\n', 'The total value = $1820.00\n'] >>> myfile.close()
這個示例演示了如何在文件中移動,一次讀取一行,或者顯式地用 seek 方法移動文件位置指示器。首先,用 readline 方法在文件行中移動。當(dāng)?shù)竭_(dá)文件末尾時(shí),readline 方法返回一個空的 string。在過了文件末尾之后,如果還用這種方式繼續(xù)讀取,并不會造成錯誤,只會返回空的 string。
然后返回文件開始的地方,并讀取另一行。 tell 方法顯示出在文件中的當(dāng)前位置(應(yīng)當(dāng)在第一行文本之后) ―― 在這個示例中,在第 13 個字符位置。通過使用這個知識,可以向 read 或readline 方法傳遞一個參數(shù),控制讀取的字符數(shù)。對于 read 方法,這個參數(shù)(在這個示例中是 17)是要從文件中讀取的字符數(shù)。但是 readline 方法在讀入指定數(shù)量的字符后,還會繼續(xù)讀取,直到行尾。在這個示例中,它讀取第一行和第二行文本。
寫入數(shù)據(jù)
迄今為止的示例都側(cè)重于讀取數(shù)據(jù),而不是寫入數(shù)據(jù)。但是如清單 6 所示,一旦了解了使用 file 對象的基礎(chǔ)知識,寫入也很容易。
清單 6. 寫入數(shù)據(jù)
>>> mydata = ['Hello World!', 'The total value = $1820.00'] >>> myfile = open('testit.txt', 'w') >>> for line in mydata: ... myfile.write(line + '\n') ... >>> myfile.close() >>> myfile = open("testit.txt") >>> myfile.read() 'Hello World!\nThe total value = $1820.00\n' >>> myfile.close() >>> myfile = open("testit.txt", "r+") >>> for line in mydata: ... myfile.write(line + '\n') ... >>> myfile.seek(0) >>> myfile.read() 'Hello World!\nThe total value = $1820.00\n' >>> myfile.close() >>> myfile = open("testit.txt", "r+a") >>> myfile.read() 'Hello World!\nThe total value = $1820.00\n' >>> for line in mydata: ... myfile.write(line + '\n') ... >>> myfile.seek(0) >>> myfile.read() 'Hello World!\nThe total value = $1820.00\nHello World!\nThe total value = $1820.00\n' >>> myfile.close()
要把數(shù)據(jù)寫入文件,必須先創(chuàng)建 file 對象。但是,在這情況下,必須用 'w' 模式標(biāo)記指定要寫入文件。在這個示例中,把 mydata list 的內(nèi)容寫入文件,關(guān)閉文件,然后重新打開文件,這樣就可以讀取內(nèi)容了。
但是,通常情況下,想要同時(shí)讀取文件和寫入文件,所以這個示例的下一部分用 'r+' 模式重新打開文件。因?yàn)槟軌驅(qū)懭胛募皇翘砑樱晕募唤財(cái)唷J紫龋?mydata list 的內(nèi)容寫入文件,然后把文件指針重新定位到文件開頭,并讀入內(nèi)容。然后這個示例關(guān)閉文件,并用讀取和添加模式 "r+a" 重新打開文件。正如示例代碼所示,文件內(nèi)容現(xiàn)在是兩個寫入操作的結(jié)果(文本是重復(fù)的)。
處理二進(jìn)制數(shù)據(jù)
前面所有的示例都處理文本數(shù)據(jù)或字符數(shù)據(jù):寫入和讀取字符 string。但是,在某些情況下,例如在處理整數(shù)或壓縮文件時(shí),需要能夠讀取和寫入二進(jìn)制數(shù)據(jù)。在創(chuàng)建 file 對象時(shí),通過把 'b' 添加到文件模式中,可以很容易地用 Python 處理二進(jìn)制數(shù)據(jù),如清單 7 所示。
清單 7. 處理二進(jìn)制數(shù)據(jù)
>>> myfile = open("testit.txt", "wb") >>> for c in range(50, 70): ... myfile.write(chr(c)) ... >>> myfile.close() >>> myfile = open("testit.txt") >>> myfile.read() '23456789:;<=>?@ABCDE' >>> myfile.close()
在這個示例中,創(chuàng)建一個合適的 file 對象,然后用從 50 到 69 的 ASCII 值寫入二進(jìn)制字符。使用 chr 方法,把 range 方法調(diào)用創(chuàng)建的整數(shù)轉(zhuǎn)變成字符。在寫完所有數(shù)據(jù)之后,關(guān)閉文件并重新打開文件進(jìn)行讀取,還是使用二進(jìn)制模式標(biāo)記。讀取文件可以證明沒有把整數(shù)寫入文件,相反,寫的是字符值。
在讀取和寫入二進(jìn)制數(shù)據(jù)時(shí),必須小心,因?yàn)椴煌钠脚_用不同的方式保存二進(jìn)制數(shù)據(jù)。如果必須處理二進(jìn)制數(shù)據(jù),最好是使用來自 Python 庫的合適對象(或者來自第三方開發(fā)人員的對象)。
讀取和寫入:最有趣的地方
這篇文章討論了在 Python 程序中如何從文件讀取數(shù)據(jù)和寫入數(shù)據(jù)到文件中。總體來說,過程很簡單:創(chuàng)建合適的 file 對象,然后按照需要讀取和寫入。但是,在使用寫入模式創(chuàng)建 file 文件,向文件寫入數(shù)據(jù)時(shí),必須注意文件的截?cái)唷H绻枰蛭募刑砑訑?shù)據(jù),應(yīng)當(dāng)在創(chuàng)建 file 對象時(shí),使用添加模式。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
