在Python文件中读取行时跳过第一行 [英] Skip first couple of lines while reading lines in Python file
本文介绍了在Python文件中读取行时跳过第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在阅读文本文件时跳过前17行。
I want to skip the first 17 lines while reading a text file.
让我们说文件看起来像:
Let's say the file looks like:
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
good stuff
我只想要好东西。我正在做的是更复杂,但这是我遇到麻烦的一部分。
I just want the good stuff. What I'm doing is a lot more complicated, but this is the part I'm having trouble with.
推荐答案
使用切片,如下所示
with open('yourfile.txt') as f:
lines_after_17 = f.readlines()[17:]
如果文件太大量加载内存:
If the file is too big to load in memory:
with open('yourfile.txt') as f:
for _ in xrange(17):
next(f)
for line in f:
# do stuff
这篇关于在Python文件中读取行时跳过第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文