簡體   English   中英

在Python中讀取和打印特定行

[英]Reading and printing specific lines in 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行。 非常感謝您的幫助,因為我已經精疲力竭了。

只需打開文件,將前六行和我們存儲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是否等於您要檢查的任何字符串,否則,我們將打印最后六行。

這是打印最后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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM