猿问

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

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 讲述的台词?


Cats萌萌
浏览 579回答 1
1回答

大话西游666

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)这样可行。接下来,尝试发布您的方法。
随时随地看视频慕课网APP

相关分类

Python
我要回答