码迷,mamicode.com
首页 > 编程语言 > 详细

python读取特定的行

时间:2016-06-22 17:18:50      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:python   linecache   readlines   

fr = open(filename)
for line in fr.readlines():
    if line.startswith("#"):
        continue
    else:
        ## do something with line
        
## could use fr.readlines()[1:]  from second line

import linecache
content_list = linecache.getlines(filename)[1:]     ## Here using getlines

fourth_line = linecache.getline(filename, 4)        ## Here using getline
## the above method is simpler.


python读取特定的行

标签:python   linecache   readlines   

原文地址:http://matrix6ro.blog.51cto.com/1746429/1791788

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!