从python中的某一行开始读取

时间:2014-10-10 03:12:06

标签: python

是否可以从某一行开始读取?在下面引用的示例中,我只想从Line04开始阅读和使用

with open (fileList[0], 'rt') as filehandle:
     for line in filehandle:
         print line

# Output:
# This is a testing file
#
# v 1.05
# v -2.15

3 个答案:

答案 0 :(得分:2)

您可以跳过前四行,使用enumerate来计算它们:

with open(fileList[0], 'rt') as filehandle:
    for line_num, line in enumerate(filehandle):
        if line_num < 4:
            continue

        print line
        # and do anything else

答案 1 :(得分:1)

lineno = 0
for line in filehandle:
    lineno = lineno + 1
    if(lineno > 4):
        print line

答案 2 :(得分:1)

这应该有效:

with open('your_file', 'rt') as filehandle:
  lines = filehandle.readlines()[4:]

for line in lines:
    print line
    #do something