欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python3實現(xiàn)從文件中讀取指定行的方法

 更新時間:2015年05月22日 11:57:06   作者:皮蛋  
這篇文章主要介紹了Python3實現(xiàn)從文件中讀取指定行的方法,涉及Python中l(wèi)inecache模塊操作文件的使用技巧,需要的朋友可以參考下

本文實例講述了Python3實現(xiàn)從文件中讀取指定行的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:

# Python的標(biāo)準(zhǔn)庫linecache模塊非常適合這個任務(wù)
import linecache
the_line = linecache.getline('d:/FreakOut.cpp', 222)
print (the_line)
# linecache讀取并緩存文件中所有的文本,
# 若文件很大,而只讀一行,則效率低下。
# 可顯示使用循環(huán), 注意enumerate從0開始計數(shù),而line_number從1開始
def getline(the_file_path, line_number):
  if line_number < 1:
    return ''
  for cur_line_number, line in enumerate(open(the_file_path, 'rU')):
    if cur_line_number == line_number-1:
      return line
  return ''
the_line = linecache.getline('d:/FreakOut.cpp', 222)
print (the_line)

希望本文所述對大家的Python程序設(shè)計有所幫助。

相關(guān)文章

最新評論