简体   繁体   中英

Reading and printing specific lines in Python

I am currently doing some GCSE computer science Python programming but I've come up against a bit of a problem, and I cannot seem to find answer.

I have a piece of code that is to read from a file and print a part of the file. My code can be seen below:

#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:

The below is a copy of the file that I am using to read from:

High 
Running
Swimming
Aerobics
Football
Tennis

Moderate
Walking
Hiking
Cleaning
Skateboarding
Basketball

As you can see the elif sub programmes all print the first 6 lines of the file but at the last else command I would like the program to print the last 6 lines of the file above. Help would be greatly appreciated as I have exhausted any ideas I have on how to do this.

Just open the file once store the first six line and us 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)

A file object returns its own iterator so after first [next(f) for _ in range(6)] the file pointer will be at the seventh line so we just start from there in the else. You do the same thing every time if client_ID == "NeQua" etc.. so using in to test for membership will see if client_ID is equal to any of the strings you are checking, if not we print the last six lines.

This is one way to print the last 6 lines:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM