如何在Python 3中从文本文件获取行和下一行

时间:2018-10-10 06:56:28

标签: python python-3.x

CHILD:     I saw giraffe swimming in the pool to get that ball.
           Hi everyone!
Teacher:   What is your name. what did you see?
CHILD:     My name is John.I am a boy. I go to school everyday.I have
           two brothers and sisters
Teacher:   I saw giraffe swimming in the pool to get that ball .

我如何才能获得仅由Child讲述的台词?

1 个答案:

答案 0 :(得分:2)

with open("sample.txt", "r") as file:
    for line in file.readlines():
        if "CHILD:" in line:
            speaker = 1
        elif "Teacher:" in line:
            speaker = 2
        if speaker == 1:
            print(line)

那行得通。接下来,尝试发布您的方法。