从python第二行开始读取文件 [英] Read a file starting from the second line in python

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

问题描述

我使用python,但我不知道该怎么做.

I use python and I don't know how to do.

我想读取文件中的许多行.但是我必须从第二行开始阅读.所有文件都有不同的行,所以我不知道该怎么办.

I want to read lots of lines in files. But I have to read from second lines. All files have different lines, So I don't know how to do.

代码示例是从第一行到第16行读取的. 但是我必须从第二行到行尾读取文件. 谢谢!:)

Code example is that it read from first line to 16th lines. But I have to read files from second lines to the end of lines. Thank you!:)

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

推荐答案

您应该可以呼叫next并丢弃第一行:

You should be able to call next and discard the first line:

with open('filename') as fin:
    next(fin) # cast into oblivion
    for line in fin:
        ... # do something

这很简单,因为fin的性质,它是生成器.

This is simple and easy because of the nature of fin, being a generator.

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

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