在Python中读取和打印特定行

时间:2015-06-10 14:25:22

标签: python

我目前正在做一些GCSE计算机科学Python编程,但我遇到了一些问题,我似乎无法找到答案。

我有一段代码可以从文件中读取并打印文件的一部分。我的代码如下所示:

#Welcome Message
print("Hello and welcome to the client activity recorder. \nHere you will be able to see and asign exercise levels to clients.")

#Open file for reading.
client_file_read = open("clientIntensity.txt","r")
print_content = client_file_read.read()
print(print_content)

#Client Selection
print("Please type the client ID of the person you wish to check what relevant activities apply:")
client_ID = input()
if client_ID == ("NeQua"):
    with open("exerciseActivities.txt") as f:
        print("For the supplied Client ID the following activities are available: \n")
        for x in range (6):
            line = f.readline()
            print(line)
            f.close
elif client_ID == ("RoDen"):
    with open("exerciseActivities.txt") as f:
        print("For the supplied Client ID the following activities are available: \n")
        for x in range (6):
            line = f.readline()
            print(line)
            f.close
elif client_ID == ("BrFre"):
    with open("exerciseActivities.txt") as f:
        print("For the supplied Client ID the following activities are available: \n")
        for x in range (6):
            line = f.readline()
            print(line)
            f.close
elif client_ID == ("KaDat"):
    with open("exerciseActivities.txt") as f:
        print("For the supplied Client ID the following activities are available: \n")
        for x in range (6):
            line = f.readline()
            print(line)
            f.close
elif client_ID == ("ViRil"):
    with open("exerciseActivities.txt") as f:
        print("For the supplied Client ID the following activities are available: \n")
        for x in range (6):
            line = f.readline()
            print(line)
            f.close
elif client_ID == ("TrGeo"):
    with open("exerciseActivities.txt") as f:
        print("For the supplied Client ID the following activities are available: \n")
        for x in range (6):
            line = f.readline()
            print(line)
            f.close
else:
    with open("exerciseActivities.txt") as f:

以下是我用来阅读的文件的副本:

High 
Running
Swimming
Aerobics
Football
Tennis

Moderate
Walking
Hiking
Cleaning
Skateboarding
Basketball

正如你所看到的,elif子程序都打印了文件的前6行,但是在最后一个else命令中,我希望程序打印上面文件的最后6行。非常感谢帮助,因为我已经用尽任何关于如何做到这一点的想法。

2 个答案:

答案 0 :(得分:1)

只需打开文件,然后存储前六行和我们in

with open("exerciseActivities.txt") as f:
    first_six = [next(f) for _ in range(6)]
    if client_ID in {"NeQua","RoDen","BrFre","KaDat","ViRil","TrGeo"}:
        print("For the supplied Client ID the following activities are available: \n")      
        for line in first_six:
            print(line)
    else:
        next(f) # skip empty line
        for line in f:
            print(line)

文件对象返回自己的迭代器,所以在第一个[next(f) for _ in range(6)]之后,文件指针将位于第七行,所以我们只是从那里开始。你每次if client_ID == "NeQua"等都做同样的事情。所以使用in来测试会员资格会看client_ID是否等于你正在检查的任何字符串,如果不是我们打印的话最后六行。

答案 1 :(得分:1)

这是打印最后6行的一种方法:

from collections import deque

with open("exerciseActivities.txt", 'r') as f:
    last6_lines = deque(f, 6)
    x = list(last6_lines)
    y = ''.join(x)
    print (y)