I'm coding for a little school project. I wanna read a .txt file and find what's between "pergunta[]" and a question mark but can't manage to make my program give me that.
I have already tried what someone suggested here but it doesn't seem to work for me, as it does not retrieve the piece of string I want and apparently does not even enter the if statement.
("perguntas" means questions)
import pyttsx3
speak = pyttsx3.init()
running = True
perguntas = open(r"C:\Users\jeana\Desktop\perguntas.txt", "r")
texto = perguntas.read()
while running:
if "pergunta5 " in texto:
data = texto.split("pergunta5 ")[1].split("?")[0]
print(data) #tried adding this line but it is never printed
speak.say(data)
speak.runAndWait()
running = False
print("um loop") #I added this just to know the code reaches this point
running = False
I expected my code to find the question that is between "pergunta[]"(5 in this case, just to simplify) and "?" and text-to-speech it, but for some reason this code simply outputs something that sounds like a "p" and no error messages. I wonder if I'm missing something that is fundamental here...
The text file looks like this:
pergunta1 Quanto é dois mais dois? R: 4 - 2
pergunta2 Quanto é cinco menos 2? R: 3 - 2
pergunta3 Quanto é cinco menos 1? R: 4 - 2
pergunta4 A peppa pig é um? R: Porco - 3
pergunta5 Qual a cor do cavalo branco do napoleão? R: Branco - 3
edit: A simpler version of my code is
text = "a lot of text with some question1 yadayadayada? question2 dayadayadaya?"
if "question1" in text:
data = text.split("question1")[1].split("?")[0]
print(data)
and the output should go:
yadayadayada