国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

利用Python中的輸入和輸出功能進行讀取和寫入的教程

2019-11-25 17:44:45
字體:
來源:轉載
供稿:網友

讀取、寫入和 Python

編寫程序的最后一個基本步驟就是從文件讀取數(shù)據和把數(shù)據寫入文件。閱讀完這篇文章之后,可以在自己的 to-do 列表中加上檢驗這個技能學習效果的任務。
簡單輸出

貫穿整個系列,一直用 print 語句寫入(輸出)數(shù)據,它默認把表達式作為 string 寫到屏幕上(或控制臺窗口上)。清單 1 演示了這一點。清單 1 重復了第一個 Python 程序 “Hello, World!”,但是做了一些小的調整。
清單 1. 簡單輸出

>>> print "Hello World!"Hello World!>>> print "The total value is = $", 40.0*45.50The 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ù)據很容易。首先,示例輸出一個簡單的 string。然后創(chuàng)建并輸出復合的 string,這個字符串是用 string 格式化技術創(chuàng)建的。

但是,在這之后,事情發(fā)生了變化,與代碼以前的版本不同。接下來的一行創(chuàng)建 file 對象,傳遞進名稱 "testit.txt" 和 'w' 字符(寫入文件)。然后使用修改過的 print 語句 ―― 兩個大于號后邊跟著容納 file 對象的變量 ―― 寫入相同的 string。但是這一次,數(shù)據不是在屏幕上顯示。很自然的問題是:數(shù)據去哪兒了?而且,這個 file 對象是什么?

第一個問題很容易回答。請查找 testit.txt 文件,并像下面那樣顯示它的內容。

% more testit.txt Hello World!The total value = $1820.00

可以看到,數(shù)據被準確地寫入文件,就像以前寫到屏幕上一樣。

現(xiàn)在,請注意清單 1 中的最后一行,它調用 file 對象的 close 方法。在 Python 程序中這很重要,因為在默認情況下,文件輸入和輸出是緩沖的;在調用 print 語句時,數(shù)據實際未被寫入;相反,數(shù)據是成批寫入的。讓 Python 把數(shù)據寫入文件的最簡單方式就是顯式地調用 close 方法。
文件對象

file 是與計算機上的文件進行交互的基本機制。可以用 file 對象讀取數(shù)據、寫入數(shù)據或把數(shù)據添加到文件,以及處理二進制或文本數(shù)據。

學習 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 構造函數(shù)或 open 方法創(chuàng)建 file 對象,open 是 file 構造函數(shù)的別名。第二個參數(shù)是可選的,它指定文件的使用方式:

  •     'r' (默認值)表示從文件讀取數(shù)據。
  •     'w' 表示要向文件寫入數(shù)據,并截斷以前的內容。
  •     'a' 表示要向文件寫入數(shù)據,但是添加到當前內容尾部。
  •     'r+' 表示對文件進行讀寫操作(刪除以前的所有數(shù)據)。
  •     'r+a' 表示對文件進行讀寫操作(添加到當前內容尾部)。
  •     'b' 表示要讀寫二進制數(shù)據。

這篇文章的第一個代碼清單向文件寫入數(shù)據。現(xiàn)在,清單 3 顯示如何把這個數(shù)據讀入 Python 程序,并解析文件的內容。
清單 3. 從文件讀取數(shù)據

>>> 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 strHello 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ù)據,首先要創(chuàng)建合適的 file 對象 ―― 在這個示例中,文件對象打開 testit.txt 文件,并用 read 方法讀取內容。這個方法把整個文件讀入一個 string,然后在程序中把這個字符串輸出到控制臺。在對 read 方法的第二個調用中,試圖把值分配給 str 變量,結果返回一個空的 string。這是因為第一個 read 操作讀入了整個文件。當試圖再次讀取內容時,已經到了文件末尾,所以什么也讀不到。

這個問題的解決方案也很簡單:讓 file 對象返回文件的開頭。回到開頭要通過 seek 方法進行,它接受一個參數(shù),表示要從文件中的什么位置開始讀取或寫入(例如,0 代表文件開頭)。seek 方法支持更復雜的操作,但是可能會有危險。對于目前來說,我們還堅持采用簡單方式。

現(xiàn)在回到了文件的開始之處,可以把文件內容讀入 string 變量并對 string 做適當?shù)亟馕觥U堊⒁猓谖募校兄g用新行(或行結束)字符區(qū)分。如果試著在 string 上調用 split 方法,它會在空白字符(例如空格)處進行拆分。為了讓方法根據新行字符拆分各行,必須顯式地指定新行字符。然后可以拆分 string 并在 for 循環(huán)中對文件的行進行迭代。

看起來僅僅從文件中讀取和處理一行內容都有許多工作要做。Python 要讓簡單的事情變容易,所以您可能想知道這個任務有沒有快捷方式可用。如清單 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 演示了讀取和解析文本文件行的三種技術。首先,打開文件并把它分配給變量。然后調用 readlines 方法,把整個文件讀入內存并把內容拆分成 string 列表。for 循環(huán)在 string 列表上進行迭代,一次輸出一行。

第二個 for 循環(huán)通過使用 file 對象的隱式變量(也就是說,變量不是顯式創(chuàng)建的),對這個過程稍做了點兒簡化。打開文件和讀取文件內容一次完成,生成的效果與第一個顯式示例相同。最后一個示例進一步做了簡化,并演示了直接在 file 對象上進行迭代的能力(請注意,這是 Python 的一個新特性,所以在您的計算機上可能無法工作)。在這個示例中,創(chuàng)建隱式 file 對象,然后 Python 做余下的工作,允許對文件中的全部行進行迭代。

但是,有些時候,在從文件讀取數(shù)據時,可能想要更好的控制級別。在這種情況下,應當使用 readline 方法,如清單 5 所示。
清單 5. 讀取數(shù)據

>>> 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 方法在文件行中移動。當?shù)竭_文件末尾時,readline 方法返回一個空的 string。在過了文件末尾之后,如果還用這種方式繼續(xù)讀取,并不會造成錯誤,只會返回空的 string。

然后返回文件開始的地方,并讀取另一行。 tell 方法顯示出在文件中的當前位置(應當在第一行文本之后) ―― 在這個示例中,在第 13 個字符位置。通過使用這個知識,可以向 read 或readline 方法傳遞一個參數(shù),控制讀取的字符數(shù)。對于 read 方法,這個參數(shù)(在這個示例中是 17)是要從文件中讀取的字符數(shù)。但是 readline 方法在讀入指定數(shù)量的字符后,還會繼續(xù)讀取,直到行尾。在這個示例中,它讀取第一行和第二行文本。
寫入數(shù)據

迄今為止的示例都側重于讀取數(shù)據,而不是寫入數(shù)據。但是如清單 6 所示,一旦了解了使用 file 對象的基礎知識,寫入也很容易。
清單 6. 寫入數(shù)據

>>> 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ù)據寫入文件,必須先創(chuàng)建 file 對象。但是,在這情況下,必須用 'w' 模式標記指定要寫入文件。在這個示例中,把 mydata list 的內容寫入文件,關閉文件,然后重新打開文件,這樣就可以讀取內容了。

但是,通常情況下,想要同時讀取文件和寫入文件,所以這個示例的下一部分用 'r+' 模式重新打開文件。因為能夠寫入文件,而不是添加,所以文件會被截斷。首先,把 mydata list 的內容寫入文件,然后把文件指針重新定位到文件開頭,并讀入內容。然后這個示例關閉文件,并用讀取和添加模式 "r+a" 重新打開文件。正如示例代碼所示,文件內容現(xiàn)在是兩個寫入操作的結果(文本是重復的)。
處理二進制數(shù)據

前面所有的示例都處理文本數(shù)據或字符數(shù)據:寫入和讀取字符 string。但是,在某些情況下,例如在處理整數(shù)或壓縮文件時,需要能夠讀取和寫入二進制數(shù)據。在創(chuàng)建 file 對象時,通過把 'b' 添加到文件模式中,可以很容易地用 Python 處理二進制數(shù)據,如清單 7 所示。
清單 7. 處理二進制數(shù)據

>>> 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 值寫入二進制字符。使用 chr 方法,把 range 方法調用創(chuàng)建的整數(shù)轉變成字符。在寫完所有數(shù)據之后,關閉文件并重新打開文件進行讀取,還是使用二進制模式標記。讀取文件可以證明沒有把整數(shù)寫入文件,相反,寫的是字符值。

在讀取和寫入二進制數(shù)據時,必須小心,因為不同的平臺用不同的方式保存二進制數(shù)據。如果必須處理二進制數(shù)據,最好是使用來自 Python 庫的合適對象(或者來自第三方開發(fā)人員的對象)。

讀取和寫入:最有趣的地方

這篇文章討論了在 Python 程序中如何從文件讀取數(shù)據和寫入數(shù)據到文件中。總體來說,過程很簡單:創(chuàng)建合適的 file 對象,然后按照需要讀取和寫入。但是,在使用寫入模式創(chuàng)建 file 文件,向文件寫入數(shù)據時,必須注意文件的截斷。如果需要向文件中添加數(shù)據,應當在創(chuàng)建 file 對象時,使用添加模式。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 敦煌市| 抚宁县| 湟中县| 达孜县| 桓仁| 彭阳县| 澄江县| 枝江市| 太原市| 微博| 忻州市| 措勤县| 芮城县| 镇雄县| 安吉县| 左云县| 重庆市| 富宁县| 卫辉市| 岢岚县| 衡阳县| 庆元县| 泸州市| 磐石市| 桂平市| 府谷县| 嘉善县| 如皋市| 涟水县| 阿城市| 三河市| 本溪市| 梨树县| 延长县| 灌阳县| 东丰县| 德安县| 古丈县| 司法| 怀来县| 三门县|