Reading and printing speciffic lines in Python -


i doing gcse computer science python programming i've come against bit of problem, , cannot seem find answer.

i have piece of code read file , print part of file. code can seen below:

#welcome message print("hello , welcome client activity recorder. \nhere able see , asign exercise levels clients.")  #open file reading. client_file_read = open("clientintensity.txt","r") print_content = client_file_read.read() print(print_content)  #client selection print("please type client id of person wish check relevant activities apply:") client_id = input() if client_id == ("nequa"):     open("exerciseactivities.txt") f:         print("for supplied client id following activities available: \n")         x in range (6):             line = f.readline()             print(line)             f.close elif client_id == ("roden"):     open("exerciseactivities.txt") f:         print("for supplied client id following activities available: \n")         x in range (6):             line = f.readline()             print(line)             f.close elif client_id == ("brfre"):     open("exerciseactivities.txt") f:         print("for supplied client id following activities available: \n")         x in range (6):             line = f.readline()             print(line)             f.close elif client_id == ("kadat"):     open("exerciseactivities.txt") f:         print("for supplied client id following activities available: \n")         x in range (6):             line = f.readline()             print(line)             f.close elif client_id == ("viril"):     open("exerciseactivities.txt") f:         print("for supplied client id following activities available: \n")         x in range (6):             line = f.readline()             print(line)             f.close elif client_id == ("trgeo"):     open("exerciseactivities.txt") f:         print("for supplied client id following activities available: \n")         x in range (6):             line = f.readline()             print(line)             f.close else:     open("exerciseactivities.txt") f: 

the below copy of file using read from:

high  running swimming aerobics football tennis  moderate walking hiking cleaning skateboarding basketball 

as can see elif sub programmes print first 6 lines of file @ last else command program print last 6 lines of file above. appreciated have exhausted ideas have on how this.

kind regards

just open file once store first 6 line , in:

with open("exerciseactivities.txt") f:     first_six = [next(f) _ in range(6)]     if client_id in {"nequa","roden","brfre","kadat","viril","trgeo"}:         print("for supplied client id following activities available: \n")               line in first_six:             print(line)     else:         next(f) # skip empty line         line in f:             print(line) 

a file object returns own iterator after first [next(f) _ in range(6)] file pointer @ seventh line start there in else. same thing every time if client_id == "nequa" etc.. using in test membership see if client_id equal of strings checking, if not print last 6 lines.


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -