1. 程式人生 > >Python讀寫檔案小結

Python讀寫檔案小結

read()、readline()、readlines()的比較

read

特點是:讀取整個檔案,將檔案內容放到一個字串變數中。

劣勢是:如果檔案非常大,尤其是大於記憶體時,無法使用read()方法。

read()的返回值是字串,讀取的是整個檔案,包含檔案中的換行符。

readline

特點:readline()方法每次讀取一行;返回的是一個字串物件,保持當前行的記憶體

缺點:比readlines慢得多

readlinede()的返回值也是字串,每次讀取一行,也包含這一行的換行符。

readlines

特點:一次性讀取整個檔案;自動將檔案內容分析成一個行的列表

比readline要快,其返回值是一個列表,列表的元素是檔案中包含換行符的一行字串。

 

try……finally與with……as的比較

讀寫檔案的正確方式是:開啟一個檔案後,一定要關閉檔案。最簡陋的做法是:

F = open('檔案', 'r')
.
.
.
F.close()

為什麼說他簡陋呢,因為如果要讀取檔案失敗,或者其他異常發生,則無法關閉檔案,造成記憶體洩露。

於是有這麼做的:

 f = open('azusa', 'r') 
  try: 
    print ''.join(f.readlines()) 
  finally: 
    f.close()

但是這樣寫起來比較繁瑣,用with……as就好很多:

 with open('azusa', 'r') as f
    print ''.join(f.readlines()) 
  

有時候需要用到異常捕獲,使用try……finally的寫法:

 
   try: 
    f = open('sawako', 'r') 
    try: 
      print ''.join(f.readlines()) 
    finally: 
      f.close() 
   except: 
     print 'error occurs while reading file'

with……as是這樣的:

 try: 
     with open('mio', 'r') as f: 
      print ''.join(f.readlines()) 
  except: 
    print 'error occurs while reading file'

反正就是要簡單。

write、writelines和numpy.savetxt的比較

儲存資料到檔案中去,其實尋常資料用write、writelines就可以了,但是對於需要做簡單處理,然後儲存的方法採用numpy的savetxt要好用很多。

write()

需要傳入一個字串做為引數,否則會報錯。

writelines()

writelines()既可以傳入字串又可以傳入一個字元序列,並將該字元序列寫入檔案,但是必須傳入的是字元序列,不能是數字序列。所以在用writelines儲存數字時,還需要做轉換。太費勁了!!!

numpy.savetxt()

 

savetxt的函式原型是:

numpy.savetxt(fnameXfmt='%.18e'delimiter=' 'newline='\n'header=''footer=''comments='# 'encoding=None)

fname : filename or file handle
If the filename ends in .gz, the file is automatically saved in compressed gzip format. loadtxt understands gzipped files transparently.

X : 1D or 2D array_like
Data to be saved to a text file.

fmt : str or sequence of strs, optional
A single format (%10.5f), a sequence of formats, or a multi-format string, e.g. ‘Iteration %d – %10.5f’, in which case delimiter is ignored. For complex X, the legal options for fmt are:

a single specifier, fmt=’%.4e’, resulting in numbers formatted like ‘ (%s+%sj)’ % (fmt, fmt)
a full string specifying every real and imaginary part, e.g. ‘ %.4e %+.4ej %.4e %+.4ej %.4e %+.4ej’ for 3 columns
a list of specifiers, one per column - in this case, the real and imaginary part must have separate specifiers, e.g. [‘%.3e + %.3ej’, ‘(%.15e%+.15ej)’] for 2 columns
delimiter : str, optional
String or character separating columns.

newline : str, optional
String or character separating lines.

New in version 1.5.0.

header : str, optional
String that will be written at the beginning of the file.

New in version 1.7.0.

footer : str, optional
String that will be written at the end of the file.

New in version 1.7.0.

comments : str, optional
String that will be prepended to the header and footer strings, to mark them as comments. Default: ‘# ‘, as expected by e.g. numpy.loadtxt.

New in version 1.7.0.

encoding : {None, str}, optional
Encoding used to encode the outputfile. Does not apply to output streams. If the encoding is something other than ‘bytes’ or ‘latin1’ you will not be able to load the file in NumPy versions < 1.14. Default is ‘latin1’.

New in version 1.14.0.

其中引數fname是檔名,X是需要儲存的一維或二維陣列,這兩個是必須的。只指定這兩個引數,儲存的資料是科學計數法的,而且用18位,這就很煩人。新增fmt引數即可:fmt='%s'。