Python从第二行到第十五行读取文本文件 [英] Python read text file from second line to fifteenth

查看:535
本文介绍了Python从第二行到第十五行读取文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文件,我需要从秒行到第15行进行读取. 我尝试了一些方法,但是没有方法对我有用... 如果有人可以帮助我,我将很高兴... 非常感谢!

I have a text file and I need to read from the seconds line to to 15th line including. I've tried some methods but no method worked for me... I'd be happy if anyone could help me ... thanks a lot!

推荐答案

使用itertools.islice:

from itertools import islice
with open('filename') as fin:
    for line in islice(fin, 1, 16):
        print line

这篇关于Python从第二行到第十五行读取文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆